我想根据当前的UI文化在运行时更改 View 位置。如何使用默认的Web窗体 View 引擎实现此目的?

基本上我想知道如何使用WebFormViewEngine实现custom IDescriptorFilter中的Spark

还有其他 View 引擎可以让我对 View 位置进行运行时控制吗?

编辑:我的URL应该遵循{lang}/{controller}/{action}/{id}。我不需要依赖语言的 Controller ,并且 View 已随资源本地化。但是,在某些语言中,很少有观点会有所不同。因此,我需要告诉 View 引擎首先查找特定于语言的文件夹。

最佳答案

一个简单的解决方案是,从您的Appication_Start中获取ViewEngine集合中的相应ViewEngines.Engines,并更新其 ViewLocationFormats 数组和 PartialViewLocationFormats 。没有黑客:默认情况下它是读/写的。

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
}

Razor looks like this的默认值:
ViewLocationFormats = new string[]
{
    "~/Views/{1}/{0}.cshtml",
    "~/Views/{1}/{0}.vbhtml",
    "~/Views/Shared/{0}.cshtml",
    "~/Views/Shared/{0}.vbhtml"
};

注意,您可能还想更新 PartialViewLocationFormats

09-25 18:18
查看更多