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?
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.
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.
@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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With