Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using TagHelpers in Area Views

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

like image 940
Steve McNiven-Scott Avatar asked Sep 23 '16 04:09

Steve McNiven-Scott


People also ask

What is one of the many ways for a tag helper to be recognized on a Razor page?

There are many wonderful things that you can do with a tag helper, but you need to register your tag helpers with Razor, even the Microsoft tag helpers, in order for Razor to be able to spot these tag helpers in the markup and to be able to call into the code that processes the tag helper.

What is the difference between tag helper vs HTML helper?

Tag Helpers are attached to HTML elements inside your Razor views and can help you write markup that is both cleaner and easier to read than the traditional HTML Helpers. HTML Helpers, on the other hand, are invoked as methods that are mixed with HTML inside your Razor views.

What happens when you apply the tag helper opt out character?

The opt-out character (“!”) is used to disable the Tag Helper at the element level. With the opt-out character, the HTML will not be generated for the label tag in the above case. We can use this opt-out character if we want to conditionally control rendering of the HTML elements.

Which of the following are valid TagHelpers used in the ASP NET core Cshtml pages?

@addTagHelper makes Tag Helpers available cshtml , which by default is inherited by all files in the Pages folder and subfolders; making Tag Helpers available. The code above uses the wildcard syntax ("*") to specify that all Tag Helpers in the specified assembly (Microsoft. AspNetCore.

How do I add a tag helper to a view?

If you create a new ASP.NET Core web app named AuthoringTagHelpers, the following Views/_ViewImports.cshtml file will be added to your project: The @addTagHelper directive makes Tag Helpers available to the view.

What is the difference between HTML helpers and tag helpers?

In many cases, HTML Helpers provide an alternative approach to a specific Tag Helper, but it's important to recognize that Tag Helpers don't replace HTML Helpers and there's not a Tag Helper for each HTML Helper. Tag Helpers compared to HTML Helpers explains the differences in more detail.

What is @addtaghelper in MVC?

The @addTagHelper makes the built-in tag helpers to available in the application which are defined in an assembly called Microsoft.AspNetCore.Mvc.TagHelpers. Here the wildcard “*” specifies that all the Tag Helpers are made available. Let us understand this with an example.

How to use tag helpers in Salesforce?

In order to use Tag Helpers first import the @addTagHelper directive in the _ViewImport.cshtml file. Along with the @addTagHelper directive, we also add the model namespace using the @using directive. So, modify the _ViewImport.cshtml file as shown below which you can find within the Views folder. Then modify the Index view as shown below.


2 Answers

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/.

like image 72
adem caglin Avatar answered Oct 17 '22 23:10

adem caglin


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

like image 3
Dezzamondo Avatar answered Oct 18 '22 00:10

Dezzamondo