gpt4 book ai didi

.net - 如何在ASP.NET MVC中更改默认 View 位置方案?

转载 作者:行者123 更新时间:2023-12-03 08:48:09 24 4
gpt4 key购买 nike

我想根据当前的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

关于.net - 如何在ASP.NET MVC中更改默认 View 位置方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/909794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com