i am new in MVC and very curious about to know that how could i change view & partial view location.
we know that view & partial view store in view folder. if my controller name is home then view must be store in home folder inside view folder and all parial view store in shared folder. i like to know how can i change View & partial view default location ?
1) suppose my controller name is product but i want to store the corresponding view in myproduct folder.......guide me what i need to do to make everything works fine.
2) i want to store all my partial view in partial folder inside view folder and want to load all partial view from there. so guide me what i need to do to make everything works fine.
basicall how could i instruct controller to load view & partial view from my folder without mentioning path. looking for good discussion. thanks
Reset All Views to Default Ensure Outlook is closed. 2. From the Run command (Windows Key + R), type outlook.exe /cleanviews and click OK.
If your client is a newer version of outlook, in order to avoid problems as much as possible, it is recommended that you uncheck this option(File>General). In addition, as I know, there's such a command to reset the view settings: win+R> type: outlook /cleanviews. (This command will delete all custom view settings.
If you want to have a special views locations for specific controllers, in your case you want ProductController views to go to MyProduct folder, you need to to override FindView
and FindPartialView
methods of 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 maybe want to prepend "My" to every controller views folder, your view engine 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());
}
You can modify RazorViewEngine
's ViewLocationFormats
and PartialViewLocationFormats
properties 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} would correspond to controller and {0} to view name, you can look at existing properties to make sure.
If you want to keep existing search locations you need to copy them into your new array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With