以什么方式在route.config文件中搜索视图。我想要搜索视图的顺序。 e.g。
~/Views//Home/Index
~/Views/Shared/Home/Index
答案 0 :(得分:0)
默认情况下,MVC视图引擎按从上到下的顺序搜索这些位置中的可用视图cshtml文件:
~/Views/ControllerName/ActionName.cshtml
~/Views/Shared/ActionName.cshtml
~/Views/Shared/LayoutName.cshtml
(用于布局文件)
更改或重新排序视图引擎搜索方法需要创建一个像这样的新类:
public class CustomViewSearch : RazorViewEngine
{
public CustomViewSearch()
{
MasterLocationFormats = new[]
{
"~/Views/Shared/{0}.cshtml"
};
ViewLocationFormats = new[]
{
// you can change view search order here
// {0} = action name, {1} = controller name
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{1}/{0}.cshtml"
};
PartialViewLocationFormats = ViewLocationFormats;
FileExtensions = new[]
{
"cshtml"
};
}
}
然后,将自定义视图搜索方法放在Global.asax
方法内的Application_Start
方法:
protected void Application_Start()
{
// remove all existing view search methods if you want
ViewEngines.Engines.Clear();
// add your custom view search method here
ViewEngines.Engines.Add(new CustomViewSearch());
}
欢迎任何建议。