Adding sub-directory to "View/Shared" folder in ASP.Net MVC and calling the view

asp.netasp.net Mvcasp.net Mvc-2asp.net Mvc-3Razor

asp.net Problem Overview


I'm currently developing a site using ASP.Net MVC3 with Razor. Inside the "View/Shared" folder, I want to add a subfolder called "Partials" where I can place all of my partial views (for the sake of organizing the site better.

I can do this without a problem as long as I always reference the "Partials" folder when calling the views (using Razor):

@Html.Partial("Partials/{ViewName}")

My question is if there is a way to add the "Partials" folder to the list that .Net goes through when searching for a view, this way I can call my view without having to reference the "Partials" folder, like so:

@Html.Partial("{ViewName}")

Thanks for the help!

asp.net Solutions


Solution 1 - asp.net

Solved this. To add the "Shared/Partials" sub directory I created to the list of locations searched when trying to locate a Partial View in Razor using:

@Html.Partial("{NameOfView}")

First create a view engine with RazorViewEngine as its base class and add your view locations as follows. Again, I wanted to store all of my partial views in a "Partials" subdirectory that I created within the default "Views/Shared" directory created by MVC.

public class RDDBViewEngine : RazorViewEngine
{
    private static readonly string[] NewPartialViewFormats = 
    { 
        "~/Views/{1}/Partials/{0}.cshtml",
        "~/Views/Shared/Partials/{0}.cshtml"
    };

    public RDDBViewEngine()
    {
        base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
    }       

}

Note that {1} in the location format is the Controller name and {0} is the name of the view.

Then add that view engine to the MVC ViewEngines.Engines Collection in the Application_Start() method in your global.asax:

ViewEngines.Engines.Add(new RDDBViewEngine()); 

Solution 2 - asp.net

Thank you for your answers. This has organized my Shared folder, but why do you create a new type of view engine? I just made a new RazorViewEngine, set it's PartialViewLocationFormats and added it to the list of ViewEngines.

ViewEngines.Engines.Add(new RazorViewEngine
{
    PartialViewLocationFormats = new string[]
    {
        "~/Views/{1}/Partials/{0}.cshtml",
        "~/Views/Shared/Partials/{0}.cshtml"
    }
});

Solution 3 - asp.net

It´s nice to custom the view engine, but if you just want to have a subfolder por partials you don´t need that much...

Just use the full path to the partial view, as done for the Layout View:

@Html.Partial("/Views/Shared/Partial/myPartial.cshtml")

Hope it helps someone...

Solution 4 - asp.net

If you are doing this in ASP.NET Core, simple go to the Startup class, under ConfigureServices method, and put

services.AddMvc()
    .AddRazorOptions(opt => {
        opt.ViewLocationFormats.Add("/Views/{1}/Partials/{0}.cshtml");
        opt.ViewLocationFormats.Add("/Views/Shared/Partials/{0}.cshtml");
    });

Solution 5 - asp.net

I've updated lamarant's excellent answer to include Areas:

public class RDDBViewEngine : RazorViewEngine
{
    private static readonly string[] NewPartialViewFormats =
    {
        "~/Views/{1}/Partials/{0}.cshtml",
        "~/Views/Shared/Partials/{0}.cshtml"
    };

    private static List<string> AreaRegistrations;

    public RDDBViewEngine()
    {
        AreaRegistrations = new List<string>();

        BuildAreaRegistrations();

        base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
        base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(areaRegistrations).ToArray();
    }

    private static void BuildAreaRegistrations()
    {
        string[] areaNames = RouteTable.Routes.OfType<Route>()
            .Where(d => d.DataTokens != null && d.DataTokens.ContainsKey("area"))
            .Select(r => r.DataTokens["area"].ToString()).ToArray();

        foreach (string areaName in areaNames)
        {
            AreaRegistrations.Add("~/Areas/" + areaName + "/Views/Shared/Partials/{0}.cshtml");
            AreaRegistrations.Add("~/Areas/" + areaName + "/Views/{1}/Partials/{0}.cshtml");
        }
    }
}

Then don't forget to include in your application start:

public class MvcApplication : System.Web.HttpApplication
{
 
    protected void Application_Start()
    {
        ...

        ViewEngines.Engines.Add(new RDDBViewEngine()); 
    }
}

Solution 6 - asp.net

You can also update the partialview-location-formats of the registered RazorViewEngine. Place the below code in Application_Start in Global.asax.

 RazorViewEngine razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().FirstOrDefault();
        if (razorEngine != null)
        {
            string[] newPartialViewFormats = new[] { 
                    "~/Views/{1}/Partials/{0}.cshtml",
                    "~/Views/Shared/Partials/{0}.cshtml"
            };
            razorEngine.PartialViewLocationFormats =
                razorEngine.PartialViewLocationFormats.Union(newPartialViewFormats).ToArray();
        }

Solution 7 - asp.net

You can create register your own view engine that derives from whatever view engine your are using (Webforms/Razor) and specify whatever locations you want in the constructor or just add them to the list of already existing locations:

this.PartialViewLocationFormats = viewLocations;

Then in application start you would add your view engine like so: ViewEngines.Engines.Add(new MyViewEngineWithPartialPath());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlamarantView Question on Stackoverflow
Solution 1 - asp.netlamarantView Answer on Stackoverflow
Solution 2 - asp.netDavid CabinessView Answer on Stackoverflow
Solution 3 - asp.netrepeatdomiauView Answer on Stackoverflow
Solution 4 - asp.netsaluceView Answer on Stackoverflow
Solution 5 - asp.netmcfeaView Answer on Stackoverflow
Solution 6 - asp.netSridharVenkatView Answer on Stackoverflow
Solution 7 - asp.netJoe CartanoView Answer on Stackoverflow