如何更改默认视图位置方案的ASP.NET MVC? [英] How to change default view location scheme in ASP.NET MVC?
问题描述
我想根据当前的UI文化在运行时更改视图位置。我怎样才能做到这一点使用默认Web窗体视图引擎?
I want to change view locations at runtime based on current UI culture. How can I achieve this with default Web Form view engine?
基本上,我想知道用 WebFormViewEngine
如何实现的东西是什么<一个href="http://sparkviewengine.com/documentation/viewlocations#CustomIDescriptorFilterimplementations">custom IDescriptorFilter 在星火。
Basically I want to know how implement with WebFormViewEngine
something what is custom IDescriptorFilter in Spark.
有另一种观点引擎,它让我运行控制视图位置?
Is there other view engine which gives me runtime control over view locations?
编辑:我的网址应该看起来如下 {语言} / {控制器} / {行动} / {ID}
。我不需要语言相关的控制器和视图本地化的资源。但是少数人的意见会在某些语言不同。所以,我需要告诉视图引擎的外观的语言特定的文件夹第一。
My URLs should looks following {lang}/{controller}/{action}/{id}
. I don't need language dependent controllers and views are localized with resources. However few of the views will be different in some languages. So I need to tell view engine to looks to the language specific folder first.
推荐答案
一个简单的解决办法是,在你的 Appication_Start
弄个合适视图引擎
从 ViewEngines.Engines
收集和更新其<一个href="http://msdn.microsoft.com/en-us/library/system.web.mvc.virtualpathproviderviewengine.viewlocationformats.aspx"><$c$c>ViewLocationFormats$c$c>阵列和<一href="http://msdn.microsoft.com/en-us/library/system.web.mvc.virtualpathproviderviewengine.partialviewlocationformats.aspx"><$c$c>PartialViewLocationFormats$c$c>.无两轮牛车:它是由默认的读/写
A simple solution would be to, in your Appication_Start
get hold of the appropriate ViewEngine
from the ViewEngines.Engines
collection and update its ViewLocationFormats
array and PartialViewLocationFormats
. No hackery: it's read/write by default.
protected void Application_Start()
{
...
// Allow looking up views in ~/Features/ directory
var razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().First();
razorEngine.ViewLocationFormats = razorEngine.ViewLocationFormats.Concat(new string[]
{
"~/Features/{1}/{0}.cshtml"
}).ToArray();
...
// also: razorEngine.PartialViewLocationFormats if required
}
剃刀<一个默认的href="https://aspnetwebstack.$c$cplex.com/SourceControl/latest#src/System.Web.Mvc/RazorViewEngine.cs">looks像这样:
ViewLocationFormats = new string[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};
注意,您可能需要更新<一个href="http://msdn.microsoft.com/en-us/library/system.web.mvc.virtualpathproviderviewengine.partialviewlocationformats.aspx"><$c$c>PartialViewLocationFormats$c$c>也。
Note that you may want to update PartialViewLocationFormats
also.
这篇关于如何更改默认视图位置方案的ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!