Using TagHelpers in Area Views
Asked Answered
A

2

9

I spent the last hour refactoring to use Areas, now all my Views don't seem to have function taghelpers :/

So this is what's in the Index.cshtml

       <div class="btn-group">
           <a asp-controller="Survey" asp-area="Admin" asp-action="Create" class="btn btn-primary">Create New</a>
       </div>

...and this is the rendered HTML :/

<div class="btn-group">
  <a asp-controller="Survey" asp-area="Admin" asp-action="Create" class="btn btn-primary">Create New</a>
</div>

Intellisense doesn't even show the asp- prefixes and syntax highlighting on the asp- attributes is also lost.

Other SO issues reference "asp-route-area" but that just renders out verbtim like the rest.

These all worked fine when they were in ~/Views/Name/Index.cshtml, move them out to ~/Areas/Name/Views/Name/ and nopers...

Any thoughts? Steve

Audiovisual answered 23/9, 2016 at 4:17 Comment(1)
Do you have a _ViewImports.cshtml in ~/Areas/Name/Views/ folder? Probably it doesn't exist.Shing
S
17

According to official docs:

The @addTagHelper directive makes Tag Helpers available to the view. In this case, the view file is Views/_ViewImports.cshtml, which by default is inherited by all view files in the Views folder and sub-directories; making Tag Helpers available. The code above uses the wildcard syntax (“*”) to specify that all Tag Helpers in the specified assembly (Microsoft.AspNetCore.Mvc.TagHelpers) will be available to every view file in the Views directory or sub-directory.

If you use one layout per area, to use built-in tag helpers you should add _ViewImports.cshtml in ~/Areas/Name/Views/ folder(If you use shared layout you don't need. See MusicStore project for shared layout example).

I guess you used one layout per area and you didn't add _ViewImports.cshtml ~/Areas/Name/Views/. Copy /Views/_ViewImports.cshtml into ~/Areas/Name/Views/.

Shing answered 23/9, 2016 at 7:10 Comment(2)
This works. but do you know how to apply this too all areas? Say I use the root Layout = "~/Views/Shared/_Layout.cshtml";Meatman
Hi, I was wondering how this would work in areas without copying the _ViewImports.cshtml file so I downloaded the demo. Turns out it doesn't work. The demo doesn't use tag helpers in the area, it uses HTMLHelpers, that's why it would seem it works. So with your _ViewStart.cshtml you always have to copy the _ViewImports.cshtml as well into your area. Kind of annoying but I haven't found another way. Or don't use tag helpers at all and go with HTMLHelpers. Which I almost like better anyway.Viperous
C
4

As it turns out, adding the _ViewImports.cshtml file to the Areas/ folder cascades the visibility of the file to all Areas/{area}/views within the folder.

So rather than:

-> Areas
--> Area1
----> Views
------> _ViewImports.cshtml
--> Area2
----> Views
------> _ViewImports.cshtml

We can just do:

-> Areas
--> Area1
--> Area2
--> _ViewImports.cshtml

Or more visually

example file structure for _ViewImports in Areas

Creon answered 22/7, 2020 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.