如何在 asp.net mvc 中更改搜索 viewLocation 的顺序?
how to change order of search viewLocation in asp.net mvc?
在我的项目中,我有一个名为 Common
的文件夹,我想 Asp.net Mvc 搜索 Common 文件夹以查找 Views,而不是共享文件夹。
也就是说,搜索位置视图的顺序如下:
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Common/Index.cshtml
~/Views/Common/Index.vbhtml
一个简单的解决方案。
在 Application_Start
事件上通过 ViewEngines.Engines
集合获取 RazorViewEngine
并更新其 ViewLocationFormats
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().First();
razorEngine.ViewLocationFormats = new string[]
{
"~/Views/Home/Index.cshtml",
"~/Views/Home/Index.vbhtml",
"~/Views/Common/{0}.cshtml",
"~/Views/Common/{0}.vbhtml"
};
}
如果只想改变顺序,可以使用Concat
方法组合默认位置和新位置。
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().First();
var newLocationPath = new string[]
{
"~/Views/Home/Index.cshtml",
"~/Views/Home/Index.vbhtml",
"~/Views/Common/{0}.cshtml",
"~/Views/Common/{0}.vbhtml"
}.Concat(razorEngine.ViewLocationFormats);
razorEngine.ViewLocationFormats = newLocationPath.ToArray();
}
您可以创建自定义 class 继承 RazorViewEngine
usin System.Web.Mvc
namespace Views.Infrastructure{
public class CustomrazorViewEngine : RazorViewEngin{
public CustomrazorViewEngine(){
ViewLocationFormats=new string[]{
"~/Views/{1}/{0}.cshtml",
"~/Views/Common/{0}.cshtml"
};
}
}
}
并在 Global.asax
proteced void Application_Start()
{
AreaRegisteration.registerAllArea();
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomrazorViewEngine());
RoutConfig.registerRoutes(RouteTable.Routes);
}
我建议这样做是为了防止其他引入的 Viewengines 的冲突:
ViewEngines.Engines.Clear();
在我的项目中,我有一个名为 Common
的文件夹,我想 Asp.net Mvc 搜索 Common 文件夹以查找 Views,而不是共享文件夹。
也就是说,搜索位置视图的顺序如下:
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Common/Index.cshtml
~/Views/Common/Index.vbhtml
一个简单的解决方案。
在 Application_Start
事件上通过 ViewEngines.Engines
集合获取 RazorViewEngine
并更新其 ViewLocationFormats
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().First();
razorEngine.ViewLocationFormats = new string[]
{
"~/Views/Home/Index.cshtml",
"~/Views/Home/Index.vbhtml",
"~/Views/Common/{0}.cshtml",
"~/Views/Common/{0}.vbhtml"
};
}
如果只想改变顺序,可以使用Concat
方法组合默认位置和新位置。
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().First();
var newLocationPath = new string[]
{
"~/Views/Home/Index.cshtml",
"~/Views/Home/Index.vbhtml",
"~/Views/Common/{0}.cshtml",
"~/Views/Common/{0}.vbhtml"
}.Concat(razorEngine.ViewLocationFormats);
razorEngine.ViewLocationFormats = newLocationPath.ToArray();
}
您可以创建自定义 class 继承 RazorViewEngine
usin System.Web.Mvc
namespace Views.Infrastructure{
public class CustomrazorViewEngine : RazorViewEngin{
public CustomrazorViewEngine(){
ViewLocationFormats=new string[]{
"~/Views/{1}/{0}.cshtml",
"~/Views/Common/{0}.cshtml"
};
}
}
}
并在 Global.asax
proteced void Application_Start()
{
AreaRegisteration.registerAllArea();
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomrazorViewEngine());
RoutConfig.registerRoutes(RouteTable.Routes);
}
我建议这样做是为了防止其他引入的 Viewengines 的冲突:
ViewEngines.Engines.Clear();