Specifying a Custom Absolute Path to Load Areas in ASP.NET Core MVC: A Guide

How do I modify the default views location to achieve the desired outcome of having the views located at /MVC/Views/ControllerName/Index(MethodName)? Additionally, the Components directory is required by default, so it should be utilized as the root folder for Views. Lastly, the ViewEngine should be included in the Startup.cs file for MVC.

Question:

To load areas from a custom directory, I am attempting to specify the absolute path (C:/WebApplication7/MS/{2}/Views/{1}/{0}.cshtml) instead of a relative path (/MS/{2}/Views/{1}/{0}.cshtml). In order to achieve this, I have tried specifying AreaLocationFormats in Startup.cs and implementing IViewLocationExpander. While both of these methods work well with relative paths, I require the ability to load views by providing the exact directory path, as my Views are located in a separate directory. Is it possible to achieve this in any way?

services.AddControllersWithViews()
                 .AddRazorOptions(options =>
                 {
                     options.AreaViewLocationFormats.Add(@"/MS/{2}/Views/{1}/{0}.cshtml");
                 });
 services.AddControllersWithViews()
                 .AddRazorOptions(options =>
                 {
                     options.ViewLocationExpanders.Add(new ViewLocationExpander());
                 });
public class ViewLocationExpander : IViewLocationExpander
    {
        public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable viewLocations)
        {
            //{2} is area, {1} is controller,{0} is the action
            string[] locations = new string[] { "/MS/{2}/Views/{1}/{0}.cshtml" };
            return locations.Union(viewLocations);          //Add mvc default locations after ours
        }
        public void PopulateValues(ViewLocationExpanderContext context)
        {
            context.Values["customviewlocation"] = nameof(ViewLocationExpander);
        }
    }



Solution 1:


To indicate a particular view path in areas, you can specify it within the

app.UseEndpoints

code.

Let’s assume that the name of my location is identified as

MyArea

.

Within the startup.cs file, in a particular method identified as

Configure

, there is some code present.

 app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
         name: "MyArea",
         pattern: "C:/WebApplication7/MS/{area:exists}/Views/{controller=Home}/{action=Index}.cshtml");
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapBlazorHub();
            });


Solution 2:


After conducting additional research, I managed to accomplish it by setting up a fresh FileProvider.

Add the line in Startup.cs and install the nuget package for Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.

services.Configure(options => {
    options.FileProviders.Add(new PhysicalFileProvider(@"C:/WebApplication7"));
});

Check out the link provided for information on setting up a file provider: https://github.com/dotnet/AspNetCore.Docs/issues/14593

Both of the mentioned approaches function properly when FileProvider is specified.

Frequently Asked Questions

Posted in Uncategorized