How to change the default view and partial view

I am new to MVC and am very curious to find out how I can change the view and partial view of the location.

we know that view and partial view are stored in the view folder. if the name of my controller is home, then the view should be stored in the home folder inside the view folder and in any parry store in the shared folder. I like to know how I can change the default view and partial layout for viewing?

1) suppose my controller name is a product, but I want to save the corresponding view in the myproduct folder ........ guide me what I need to do to make everything work fine.

2) I want to save all my partial views in a partial folder inside the view folder, and I want to load the entire partial view there. so let me know what i need to do so that everything is fine.

basicall, how can I instruct the controller to load the view and partial view from my folder without specifying a path. looking for a good discussion. thanks

+6
source share
2 answers

You can change the RazorViewEngine ViewLocationFormats and PartialViewLocationFormats in your Global.asax startup code. Something around the lines below should work:

 protected void Application_Start(object obj, EventArgs e) { var engine = ViewEngines.Engines.OfType<RazorViewEngine>().Single(); var newViewLocations = new string[] { "~/SomeOtherFolder/{1}/{0}.cshtml", "~/GlobalFolder/{0}.cshtml" }; engine.ViewLocationFormats = newViewLocations; engine.PartialViewLocationFormats = newViewLocations; } 

IIRC, {1} matches the controller and {0} to view the name, you can check the existing properties to make sure.

If you want to keep existing search locations, you need to copy them to a new array.

+9
source

If you want to have special views for specific controllers, in your case you want ProductController views to go to the MyProduct folder, you need to redefine the FindView and FindPartialView RazorViewEngine :

 public class MyRazorViewEngine : RazorViewEngine { public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { if (controllerContext.Controller is ProductController) { string viewPath = "/Views/MyProduct/" + viewName + ".cshtml"; return base.FindView(controllerContext, viewPath, masterName, useCache); } return base.FindView(controllerContext, viewName, masterName, useCache); } public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) { if (controllerContext.Controller is ProductController) { string partialViewPath = "/Views/MyProduct/Partials/" + partialViewName + ".cshtml"; return base.FindPartialView(controllerContext, partialViewPath, useCache); } return base.FindPartialView(controllerContext, partialViewName, useCache); } } 

And if you might want to add β€œMy” to each folder with the controller view, your view mechanism should look like this:

 public class MyRazorViewEngine : RazorViewEngine { public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { string viewPath = "/Views/My" + GetControllerName(controllerContext) + "/" + viewName + ".cshtml"; return base.FindView(controllerContext, viewPath, masterName, useCache); } public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) { string partialViewPath = "/Views/My" + GetControllerName(controllerContext) + "/Partials/" + partialViewName + ".cshtml"; return base.FindPartialView(controllerContext, partialViewPath, useCache); } private string GetControllerName(ControllerContext controllerContext) { return controllerContext.RouteData.Values["controller"].ToString(); } } 

And than in your Global.asax

 protected void Application_Start() { //remove unused view engines, for performance reasons as well ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new MyRazorViewEngine()); } 
+9
source

Source: https://habr.com/ru/post/955361/


All Articles