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.AddControllersWithViews();
services.Configure<RazorViewEngineOptions>(o =>
{
o.ViewLocationFormats.Clear();
o.ViewLocationFormats.Add
("/MyViewsFolder/{1}/{0}" + RazorViewEngine.ViewExtension);
o.ViewLocationFormats.Add
("/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)
{
services.AddRazorPages();
services.Configure<RazorPagesOptions>
(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)
{
services.AddRazorPages()
.WithRazorPagesRoot("/MyPagesFolder");
}
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!!