Enhanced View Locations for MVC allows you to further organize your ASP.NET MVC views without your action methods or Html.RenderPartial / Html.RenderAction elements knowing or caring about how they are organized or re-organized.

1. Begin by adding a reference to EnhancedViewLocations.dll to your ASP.NET MVC project via NuGet (http://nuget.org/List/Packages/EnhancedViewLocations) or by directly referencing it.

2. Next, define and register custom view locations in the form partial Uri's from within the Global.asax file:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters( GlobalFilters.Filters );
    RegisterRoutes( RouteTable.Routes );
 
    // Register the extra locations for views.
    RegisterCustomViewLocations();
}

private static void RegisterCustomViewLocations()
{
   // We can optionally enable the standard Razor view CS (or VB) locations 
   // so we don't always search our extra / custom areas first when
   // searching for a view.
   EnhancedViewLocator.EnableStandardRazorCSLocations();

   // Next, add two extra locations to store / arrange our views.

   // 1. in the templates folder under the standard shared view folder.
   EnhancedViewLocator.AddFolder( "views/shared/templates" );

   // 2. in the navigation folder under the standard shared view folder.
   EnhancedViewLocator.AddFolder( "views/shared/navigation" );

   // Finally, have the locator install a custom view engine to manage the lookups.
   EnhancedViewLocator.Install( ControllerBuilder.Current );
}

Now you can create the to subfolders templates and navigation in the shared views (it can actually be any folder on your site provided you register it but we recommend keeping inside the views folder). 

     

This allows you to put all of your navigation related controls / views in a separate and predictable place making the views folder much more manageable for large projects. In your views, you can still call code like this:

@Html.Partial("_LogOnPartial")

even though partial view is not in a 'standard' place.

The download for this project includes a working sample project from which the previous snippets were taken.

Last edited Sep 9, 2011 at 3:32 PM by mkennedy, version 10

Comments

No comments yet.