gpt4 book ai didi

c# - 如何实现自定义 RazorViewEngine 以在非标准位置查找 View ?

转载 作者:IT王子 更新时间:2023-10-29 04:34:41 24 4
gpt4 key购买 nike

我正在考虑实现自定义 RazorViewEngine。基本上我有两个具有相同代码库的站点。不同之处在于它们看起来不同。我想覆盖标准 View 引擎,使 MVC 在两个不同的位置查看其 View 、布局等。一个用于公司 A,另一个用于公司 B。公司 A 将包含主视图,公司 B 的 View 将覆盖这些主视图。因此,我希望 View 引擎在位置 B 中查找 View 、布局、主视图或部分 View (如果找到)然后返回,如果找不到,我希望它默认使用公司 A 的 View 作为默认 View 。显然公司 A 只会查看它自己的文件夹。

进入问题的关键:我找到了这个网站:http://www.aspnetwiki.com/mvc-3-razor:extending-the-view-engine

第一个问题,这是实现这一目标的最佳方式吗?

其次,我是否需要覆盖 CreatePartialCreateViewFindPartialFindView 方法?

更新

好的,我自己想出了第二个问题,我想覆盖的方法是 CreateViewCreatePartialView 因为此时它已经构建了 View 字符串,我可以摆弄它。

最佳答案

好的,最后我选择了这里详述的方法:http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx

感谢@Adriano 提供的答案和指导,但最终我认为这种方法更符合我的需求。下面的方法允许我保留标准功能,但可以创建一个新的更高优先级的 View 位置进行搜索。

public class Travel2ViewEngine : RazorViewEngine
{
protected BrandNameEnum BrandName;
private string[] _newAreaViewLocations = new string[] {
"~/Areas/{2}/%1Views/{1}/{0}.cshtml",
"~/Areas/{2}/%1Views/{1}/{0}.vbhtml",
"~/Areas/{2}/%1Views//Shared/{0}.cshtml",
"~/Areas/{2}/%1Views//Shared/{0}.vbhtml"
};

private string[] _newAreaMasterLocations = new string[] {
"~/Areas/{2}/%1Views/{1}/{0}.cshtml",
"~/Areas/{2}/%1Views/{1}/{0}.vbhtml",
"~/Areas/{2}/%1Views/Shared/{0}.cshtml",
"~/Areas/{2}/%1Views/Shared/{0}.vbhtml"
};

private string[] _newAreaPartialViewLocations = new string[] {
"~/Areas/{2}/%1Views/{1}/{0}.cshtml",
"~/Areas/{2}/%1Views/{1}/{0}.vbhtml",
"~/Areas/{2}/%1Views/Shared/{0}.cshtml",
"~/Areas/{2}/%1Views/Shared/{0}.vbhtml"
};

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

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

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

public Travel2ViewEngine()
: base()
{
Enum.TryParse<BrandNameEnum>(Travel2.WebUI.Properties.Settings.Default.BrandName, out BrandName);

AreaViewLocationFormats = AppendLocationFormats(_newAreaViewLocations, AreaViewLocationFormats);

AreaMasterLocationFormats = AppendLocationFormats(_newAreaMasterLocations, AreaMasterLocationFormats);

AreaPartialViewLocationFormats = AppendLocationFormats(_newAreaPartialViewLocations, AreaPartialViewLocationFormats);

ViewLocationFormats = AppendLocationFormats(_newViewLocations, ViewLocationFormats);

MasterLocationFormats = AppendLocationFormats(_newMasterLocations, MasterLocationFormats);

PartialViewLocationFormats = AppendLocationFormats(_newPartialViewLocations, PartialViewLocationFormats);
}

private string[] AppendLocationFormats(string[] newLocations, string[] defaultLocations)
{
List<string> viewLocations = new List<string>();
viewLocations.AddRange(newLocations);
viewLocations.AddRange(defaultLocations);
return viewLocations.ToArray();
}

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return base.CreateView(controllerContext, viewPath.Replace("%1", BrandName.ToString()), masterPath);
}

protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return base.CreatePartialView(controllerContext, partialPath.Replace("%1", BrandName.ToString()));
}

protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
return base.FileExists(controllerContext, virtualPath.Replace("%1", BrandName.ToString()));
}
}

然后在Gloabal.asax中注册

protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);


//Register our customer view engine to control T2 and TBag views and over ridding
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new Travel2ViewEngine());
}

关于c# - 如何实现自定义 RazorViewEngine 以在非标准位置查找 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9838766/

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