Custom Razor View Location In ASP.NET Core MVC

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.

You can create sub area in MVC. Read here for more details.

There are couple of APIs in IViewLocationExpander Interface.

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

  1. /// <summary>  
  2.     /// My view location expander  
  3.     /// </summary>  
  4.     public class MyViewLocationExpander : IViewLocationExpander  
  5.     {  
  6.   
  7.         public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context,   
  8.             IEnumerable<string> viewLocations)  
  9.         {  
  10.   
  11.             //replace the Views to MyViews..  
  12.             viewLocations = viewLocations.Select(s => s.Replace("Views""MyViews"));  
  13.   
  14.             return viewLocations;  
  15.         }  
  16.   
  17.         public void PopulateValues(ViewLocationExpanderContext context)  
  18.         {  
  19.             //nothing to do here.  
  20.         }  
  21.     }  

Register the MyViewLocationExpander in Startup class. You have to configure RazorViewEngineOptions into IServiceCollection and add custom View Expander to ViewLocationExpanders collections. 
  1. // This method gets called by the runtime. Use this method to add services to the container.  
  2.         public void ConfigureServices(IServiceCollection services)  
  3.         {  
  4.             //register the MyViewLocationExpander into ViewLocationExpanders  
  5.             services.Configure<RazorViewEngineOptions>(o => {  
  6.                 o.ViewLocationExpanders.Add(new MyViewLocationExpander());  
  7.             });  
  8.   
  9.             // Add framework services.  
  10.             services.AddMvc();  
  11.         }  

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