Konda.eu

Monthly Archives: November 2015

No comments

ASP.NET 5 Change Default Views Directory

Testing out new asp.net 5 MVC project template and integrating it into an existing project, predefined project structure didn't really suite my needs. Most of the things work if you move them into a subdirectory, Views directory isn't one of those. You need to create IViewLocationExpander and add it to appropriate service. Example bellow remaps everything into Server subdirectory.

public class ViewsLocationRemapper : IViewLocationExpander
{
    public void PopulateValues(ViewLocationExpanderContext context)
    {
        // Do nothing
    }

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        return viewLocations.Select(loc => "Server" + loc);
    } 
}

And then in Startup.cs, ConfigureServices. method, simply call:

services.Configure<RazorViewEngineOptions>(o =>
{
    o.ViewLocationExpanders.Add(new ViewsLocationRemapper());
});

Of course it can be used for simple tasks like prefixing Shader directory with _ (underscore), so that it can be easily found in Solution Explorer.