受欢迎的博客标签

Introduction

 

In this article, we'll learn how to customize Razor Engine View location using IViewLocationExpander . IViewLocationExpander takes care of modifying the View locations and how the View Engine searches for path. Here are the best practices to set custom Razor View location in MVC. This feature was introduced in ASP.NET Core MVC.

IViewLocationExpander APIs

  1. ExpandViewLocations (ViewLocationExpanderContext, IEnumerable<String>) : this is invoked by Razor View Engine to determine the possible View locations . View Engine searches path in order it is added in the View locations, so the order of View locations does matter.
  1. PopulateValues(ViewLocationExpanderContext) : this is called every time so as to populate the route values .

 

Project Solution Explorer
 

Here, we have MyViews folder instead of Views and folder structure would be the same. MyViews folder is highlighted in red circle.

 

MyViewLocationExpander

 

ExpandViewLocations gets invoked while calling the action view each time. It provides Context and list of possible View locations path. You have to change all the possible locations with new location. Here, I'm changing the "Views" to "MyViews".

/// <summary>  
    /// My view location expander  
    /// </summary>  
    public class MyViewLocationExpander : IViewLocationExpander  
    {  
  
        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context,   
            IEnumerable<string> viewLocations)  
        {  
  
            //replace the Views to MyViews..  
            viewLocations = viewLocations.Select(s => s.Replace("Views", "MyViews"));  
  
            return viewLocations;  
        }  
  
        public void PopulateValues(ViewLocationExpanderContext context)  
        {  
            //nothing to do here.  
        }  
    }  
Register the MyViewLocationExpander in Startup class. You have to configure RazorViewEngineOptions into IServiceCollection and add custom View Expander to ViewLocationExpanders collections. 
// This method gets called by the runtime. Use this method to add services to the container.  
        public void ConfigureServices(IServiceCollection services)  
        {  
            //register the MyViewLocationExpander into ViewLocationExpanders  
            services.Configure<RazorViewEngineOptions>(o => {  
                o.ViewLocationExpanders.Add(new MyViewLocationExpander());  
            });  
  
            // Add framework services.  
            services.AddMvc();  
        }

Register the MyViewLocationExpander in Startup class. You have to configure RazorViewEngineOptions into IServiceCollection and add custom View Expander to ViewLocationExpanders collections.

// This method gets called by the runtime. Use this method to add services to the container.  
        public void ConfigureServices(IServiceCollection services)  
        {  
            //register the MyViewLocationExpander into ViewLocationExpanders  
            services.Configure<RazorViewEngineOptions>(o => {  
                o.ViewLocationExpanders.Add(new MyViewLocationExpander());  
            });  
  
            // Add framework services.  
            services.AddMvc();  
        } 

 

visit  /common/pagenotfound

output

 System.InvalidOperationException: The view 'PageNotFound' was not found. The following locations were searched:
      /Themes/RootTheme/ Views/Common/PageNotFound.zh.cshtml
      /Themes/RootTheme/ Views/Common/PageNotFound.cshtml
      /Views/Common/PageNotFound.zh.cshtml
      /Views/Common/PageNotFound.cshtml
      /Themes/RootTheme/ Views/Shared/PageNotFound.zh.cshtml
      /Themes/RootTheme/ Views/Shared/PageNotFound.cshtml
      /Views/Shared/PageNotFound.zh.cshtml
      /Views/Shared/PageNotFound.cshtml
      /Pages/Shared/PageNotFound.zh.cshtml
      /Pages/Shared/PageNotFound.cshtml
      /Pages/Shared/PageNotFound.zh.cshtml
      /Pages/Shared/PageNotFound.cshtml

 

 

Summery

 

In this write-up, we learned how we can customize the Razor View location and do awesome things with Razor View Engine.

 

References

 

https://docs.microsoft.com/en-us/aspnet/core/api/microsoft.aspnetcore.mvc.razor.iviewlocationexpander