Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tag helpers do not resolve when view is outside the Views folder

With Asp .Net 5 beta 6 the tag helpers do not resolve when the view is located outside the Views folder. The folder structure that I would like is one based on function rather than on file types. The controller, view models and views for a particular function I would like grouped together in a folder that reflects the controller name. Here is an example of the folder structure that I would like in the project:

 UI
   - Account
        - AccountController.cs
        - AccountViewModels.cs
        - Login.cshtml
        - Register.cshtml
   - Students
        - StudentsController.cs
        - StudentsViewModels.cs
        - CaptureStudent.cshtml

To accomplish this I have created a custom view engine:

public class CustomUIViewEngine : RazorViewEngine
{
    public CustomUIViewEngine(IRazorPageFactory pageFactory,
       IRazorViewFactory viewFactory,
       IOptions<RazorViewEngineOptions> optionsAccessor,
       IViewLocationCache viewLocationCache) :
       base(pageFactory, viewFactory, optionsAccessor, viewLocationCache)
    {
    }
    public override IEnumerable<string> ViewLocationFormats
    {
        get
        {
            var viewLocationFormats = base.ViewLocationFormats
            .Union(new string[] { "~/UI/{1}/{0}.cshtml" });
            return viewLocationFormats;
        }
    }
}

Which I register in startup.cs as follows:

services.AddMvc().ConfigureMvcViews(options =>
        {
            options.ViewEngines.Clear();
            options.ViewEngines.Add(typeof(CustomUIViewEngine));
        });

This custom view engine does work and the view gets located and rendered, with the only problem being that the tag helpers are not correctly rendered, so instead of seeing this:

<form method="post" class="form-horizontal" role="form" action="/Account/Login">

I am seeing this in the rendered source:

<form asp-controller="Account" asp-action="Login" method="post" class="form-horizontal" role="form">

Does anyone know why the tag helpers are not rendering correctly when the view is located outside the Views folder and if there is any way to correct this in an Asp.Net 5 web application?

like image 622
BruceHill Avatar asked Sep 19 '15 15:09

BruceHill


People also ask

Why tag helpers is not working?

The first step when a tag helper isn't working is always to check the rendered HTML. Tag Helpers should be converted to proper HTML – if they're actually sat on the page as a tag helper then nothing will happen, because Chrome and Firefox don't know what the hell a tag helper is.

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 tag helper 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.

What is the benefit of cache tag helper?

The Cache Tag Helper provides the ability to improve the performance of your ASP.NET Core app by caching its content to the internal ASP.NET Core cache provider. For an overview of Tag Helpers, see Tag Helpers in ASP.NET Core. The first request to the page that contains the Tag Helper displays the current date.


1 Answers

Are you adding your TH's in the _ViewImports file, and if so are the using statements being recognized? Have you tried adding the @addTagHelper directly to a view?

Some of this is mentioned in my Authoring Tag Helpers I go into to detail on inheritance hierarchy, adding, removing in an upcoming article.

like image 153
RickAndMSFT Avatar answered Sep 23 '22 21:09

RickAndMSFT