In this article I will describe how to change the default directory path of Razor View Engine Partial Views. By default in Asp.Net MVC3 when you create a partial view, Visual Studio will by default create partial view in the controller view folder.
I am working on a project and want to put all the partial views in a separate folder. To full fill this requirement I am going to create partial views in Partial folder under Views. So that my all the partial views will be in same directory. To explain how we can achieve this lets create a MVC 3 project in Visual Studio 2010. I created an Internet project. It has default home controller. So when you run the application you can see default home page.
Lets create a folder under Views and name it Partial (as shown below). So it will hold all out partial views.
Now lets create a partial view. Right click on the folder and name it as “TestPartialView” and click Ok.
To make things simple lets fetch the system time and display in the view.
<p>Current Time: @DateTime.Now.ToShortTimeString()</p>
Now to change the default route of MVC so that it will look for all the Partial Views in this folder. Lets create a class file at the route of project. And name it “ViewEnginePath”. Add inherit it from “RazorViewEngine” base class. Then add partial view path in a string array. And add it in base “PartialViewLocationFormats” in the class constructor.
Now I add the view engine in the Global.ascx.cs in “Application_Start” method
Now render the partial view in Layout page @Html.Partial(“TestPartialView”)
Press F5 and run the application and you will the output.
Download code CustomizePartialView