Hands-On ASP.NET Core 3.1 : Learn MVC, Razor Pages, Web API, Entity Framework Core, and Blazor. Private online coaching for software developers. Click here to know more.

Change Default Location of Views and Razor Pages in ASP.NET Core

ASP.NET Core MVC web applications typically store view files under Views folder and Razor Pages are stored under Pages folder. Although this default location is what you want in most of the applications, at times you may want to store views and pages in some different folder. To that end this short article shows how to accomplish just that.

Store MVC views in a different location

In ASP.NET Core MVC bt default views are stored under Views folder and are grouped according to the controller they belong to. Layouts and partials are stored in Views > Shared folder. This default folder organization is shown below:

As you can see there is HomeController and views belonging to it are placed inside Views > Home folder.

Now let's change this default location to a customer location by placing the views inside MyViewsFolder folder under project root.

If you run the application at this stage you will get an error as shown below:

As you can see, the system is trying to look for Index view under Views folder. Since we have changed the location it can't find it and throws the error.

To fix the erroe, open the Startup class and add the following code in ConfigureServices() method.

public void ConfigureServices(IServiceCollection services)

    services.Configure<RazorViewEngineOptions>(o =>
("/MyViewsFolder/{1}/{0}" + RazorViewEngine.ViewExtension);
("/MyViewsFolder/Shared/{0}" + RazorViewEngine.ViewExtension);

The Configure() method called on IServiceCollection configures RazorViewEngineOptions. Specifically, ViewLocationFormats list is modified to include new view location. Notice how the Add() method of ViewLocationFormats specifies the location formats. Here, {0} and {1} parameters are placeholders for the action name and controller name. The RazorViewEngine.ViewExtension appends .cshtml file extension to the path. Shared folder is also specified in similar way.

If you run the application after making this change, it will run as expected indicating that the system is now able to find the views.

Store Razor Pages in a different location

By default Razor Pages are stored in Pages folder under project root.

Let's change this folder name to MyPagesFolder.

Obviously the system won't be able to find Razor Pages where they are expected to be (in Pages folder) and an error will be thrown.

To fix the error, open Startup class again and add this code in ConfigureServices() method.

public void ConfigureServices(IServiceCollection services)

(options => options.RootDirectory = "/MyPagesFolder");

This time you configure RazorPagesOptions using the Configure() method of IServiceCollection. Specifically you set the RootDirectory property to /MyPagesFolder. After this configuration the application will be able to find the Razor Pages at their new location.

There is a shortcut to this call:

public void ConfigureServices(IServiceCollection services)

You can also use WithRazorPagesRoot() extension method and specify the new location in the parameter. This will give the same effect as the Configure() method discussed earlier.

That's it for now! Keep coding!!

Bipin Joshi is an independent software consultant, trainer, author, yoga mentor, and meditation teacher. He has been programming, meditating, and teaching for 24+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 09 September 2019

Tags : ASP.NET ASP.NET Core .NET Framework C# Visual Studio Configuration

Subscribe to our newsletter

Get monthly email updates about new articles, tutorials, code samples, and how-tos getting added to our knowledge base.


Receive Weekly Updates