This article describes how to add a sub directory to the list of locations the ASP.Net MVC3 Razor view engine checks when searching for a partial view. Why would you want to do this? The answer is simply for the sake of organizing your file structure within the default “Views” directory that ASP.Net MVC3 creates for you.
The way you render a partial view in Razor is by making the following call:
@Html.Partial("{ViewName}")
When you make this call, MVC3′s default Razor view engine looks for the view you specified in the following default locations:
~/Views/{1}/{0}.cshtml
~/Views/{1}/{0}.vbhtml
~/Views/Shared/{0}.cshtml
~/Views/Shared/{0}.vbhtml
Note that {0} is the view name you specified and {1} is the Controller name (if specified).
If it does not find a view matching the name you specified in any of those directories, guess what? Yup, Yellow Screen of Death.
Well what if you wanted to store all of your partial views in a “Partials” directory within the “Views/Shared” folder? You could do this without a problem as long as you called your partial views from now on by prepending the view name with “Partials/”, like so:
@Html.Partial("Partials/{ViewName}")
This is of course perfectly OK, but what if you move your partial views in the future? You would then have to do a search and replace of code throughout your application.
A Better solution:
First, create your own view engine as a class in your Models directory, inheriting from the “RazorViewEngine” base class, store your view locations in a string array, and add your locations to the base PartialViewLocationFormats in your constructor like so:
public class MyViewEngine : RazorViewEngine
{
private static string[] NewPartialViewFormats = new[] {
"~/Views/{1}/Partials/{0}.cshtml",
"~/Views/Shared/Partials/{0}.cshtml"
};
public MyViewEngine ()
{
base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
}
}
Then, in your global.asax file, in you Application_Start() method, add your view engine to the applications ViewEngines collection like so:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
ViewEngines.Engines.Add(new MyViewEngine());
}
Now, when you call a partial view in Razor with @Html.Partial(…), the locations you designated will be searched in addition to the default locations.

I'm a web developer, UI/UX specialist and online strategist with over 10 years of experience in the field. I am currently freelancing and am available for hire. I have hands-on experience in all stages of the SDLC. If you or your business needs a web site, could use some help with your online strategy, or if you need to get started with SEM, please feel free to contact me.
I can help you with: Web Site Design and Development, Search Engine Optimization, Search Engine Marketing, Multivariate Testing, Metrics Analysis, Campaign Optimization, Social Site Integration, E-mail Marketing and everything else that has to do with the Web.
I work/haved worked with the following technologies: HTML5, CSS, Javascript, Ajax, JQuery, MySQL, SQL Server, ASP.Net 4.0, MVC3, Entity Framework 4, Razor, Web Services, Coldfusion, PHP, Wordpress, Flash, Google Analytics, Google Adwords, Subversion.
Thanks, excactly what I needed
Thanks for the post. Using this in my MVC project.