如何更改 ASP.NET MVC 中的默认视图位置方案? [英] How to change default view location scheme in ASP.NET MVC?

查看:20
本文介绍了如何更改 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 实现什么是 自定义 IDescriptorFilterSpark 中.

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?

我的 URL 应该遵循 {lang}/{controller}/{action}/{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 中从 ViewEngine 获取适当的 ViewEnginecode>ViewEngines.Engines 集合并更新其ViewLocationFormats 数组和PartialViewLocationFormats.无黑客:默认为读/写.

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
}

Razor 的默认看起来像这样::>

The default one for Razor looks like this:

ViewLocationFormats = new string[]
{
    "~/Views/{1}/{0}.cshtml",
    "~/Views/{1}/{0}.vbhtml",
    "~/Views/Shared/{0}.cshtml",
    "~/Views/Shared/{0}.vbhtml"
};

注意您可能想要更新PartialViewLocationFormats 也是.

Note that you may want to update PartialViewLocationFormats also.

这篇关于如何更改 ASP.NET MVC 中的默认视图位置方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆