Adding a subdirectory to the "View / Shared" folder in ASP.Net MVC and calling the view

I am 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 my partial views (for the sake of organizing the site it’s better.

I can do this without problems if I always link to the Particles folder when invoking views (using Razor):

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

My question is, is there a way to add the “Partials” folder to the list that .Net looks at when looking for a view, so I can call my view without a link to the “Partials” folder, like this:

 @Html.Partial("{ViewName}") 

Thanks for the help!

+44
asp.net-mvc asp.net-mvc-3 razor asp.net-mvc-2
Feb 11 '11 at 19:07
source share
7 answers

Decided it. To add the "Shared / Partials" subdirectory that I created to the list of search locations that I searched for when trying to find a partial view in Razor using:

 @Html.Partial("{NameOfView}") 

First, create a view engine with RazorViewEngine as the base class and add your view positions as follows. Again, I wanted to save all of my partial views in the Partials subdirectory that I created in 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 name of the controller, and {0} is the name of the view.

Then add this viewer to the MVC ViewEngines.Engines Collection in the Application_Start () method in your global.asax:

 ViewEngines.Engines.Add(new RDDBViewEngine()); 
+61
Feb 14 '11 at 20:24
source share

Thank you for your responses. This organized my shared folder, but why are you creating a new type of viewer? I just created a new RazorViewEngine , set it to PartialViewLocationFormats and added it to the ViewEngines list.

 ViewEngines.Engines.Add(new RazorViewEngine { PartialViewLocationFormats = new string[] { "~/Views/{1}/Partials/{0}.cshtml", "~/Views/Shared/Partials/{0}.cshtml" } }); 
+26
Aug 09 2018-11-21T00:
source share

It's nice to customize the view mechanism, but if you just want subfolders to be partial, you don't need it ...

Just use the full path to the partial view, as is done for the layout view:

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

Hope this helps someone ...

+13
Jun 30 2018-12-12T00:
source share

I updated Lamaran an excellent answer to include the Regions:

 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 do not forget to include at the beginning of the application:

 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { ... ViewEngines.Engines.Add(new RDDBViewEngine()); } } 
+6
Apr 11
source share

You can also update the partialview-location formats of the registered RazorViewEngine. Put the code below 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(); } 
+5
Jul 10 '13 at 17:51
source share

You can create your own viewer, which is created from any used viewer (Webforms / Razor) and specify any places you want in the constructor, or simply add them to the list of existing locations:

 this.PartialViewLocationFormats = viewLocations; 

Then when you start the application, you add your view engine like this: ViewEngines.Engines.Add(new MyViewEngineWithPartialPath());

+1
Feb 11 '11 at 19:16
source share

If you do this in ASP.NET Core, just go to the Startup class, under the ConfigureServices method and put

 services.AddMvc() .AddRazorOptions(opt => { opt.ViewLocationFormats.Add("/Views/{1}/Partials/{0}.cshtml"); opt.ViewLocationFormats.Add("/Views/Shared/Partials/{0}.cshtml"); }); 
+1
Apr 18 '17 at 13:36 on
source share



All Articles