Change ASP.NET Core Razor Pages default directory name

One of the new key feature of ASP.NET Core 2.0 is Razor Pages. Razor Pages are simple pages or views without controllers and introduced with the intent of creating page focused scenarios where there is no real logic is involved. You will find razor pages similar to ASP.NET Web Forms. They work on the convention and need to be placed in Pages folder and the extension is .cshtml. If you already have a folder named “Pages” in your solution, then it will create problems. Ideally, you would like to have a different name for the Razor page directory to avoid any issues. This post shows how to change ASP.NET Core Razor Pages default directory name.

Change ASP.NET Core Razor Pages default directory name

By default, when you create ASP.NET Core 2.0 based web application, you should see following structure. As you can see, all the razor pages are inside “Pages” directory.

Change ASP.NET Core Razor Pages default directory name

To change the name, right-click on “Pages” folder -> select rename and give a new name of your choice. Once done, run the application and as expected, it didn’t work. Well, we only renamed the folder, but forgot to inform the razor pages engine about the same.
The default directory path is defined in the RazorPagesOptions class. This class has a property named “RootDirectory”. We need to assign this new custom directory path to this property and it can be done via one line of code. Open Startup.cs and include the following line in ConfigureServices method.

public void ConfigureServices(IServiceCollection services)
{
   services.AddMvc().WithRazorPagesRoot("/MyPages");
}

Save the changes and your application would be up and running with custom directory path for razor pages.

The extension method WithRazorPagesRoot is defined in the class named MvcRazorPagesMvcBuilderExtensions and it assigns the new path to the “RootDirectory” property. Alternatively, you can also use the following code as used by the WithRazorPagesRoot extension method to change the razor pages directory name.

public void ConfigureServices(IServiceCollection services)
{
   services.AddMvc();
   services.Configure<RazorPagesOptions>(options => options.RootDirectory = "/MyPages");
}

The class MvcRazorPagesMvcBuilderExtensions also has another extension method WithRazorPagesAtContentRoot. The name explains the purpose. This is useful when you don’t want to place razor pages inside any folder and keep all of them at the root of the application.

public void ConfigureServices(IServiceCollection services)
{
   services.AddMvc().WithRazorPagesAtContentRoot();
}

OR

public void ConfigureServices(IServiceCollection services)
{
   services.AddMvc();
   services.Configure<RazorPagesOptions>(options => options.RootDirectory = "/");
}

That’s it.

Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

2 thoughts on “Change ASP.NET Core Razor Pages default directory name

Leave a Reply

Your email address will not be published. Required fields are marked *

five × four =