我如何实现一个自定义RazorViewEngine找到意见非标准位置? [英] How do I implement a custom RazorViewEngine to find views in non-standard locations?
问题描述
我期待在实现自定义的 RazorViewEngine
。基本上,我有两个站点,实际上是相同的code基地。所不同的是,他们看起来不同。我想覆盖标准视图引擎,使MVC看在两个不同的位置对于它的看法,布局等,一为A公司,另一个是B公司A公司将包含主视图和B公司的观点,将覆盖这些大师。所以我想查看引擎在B位置寻找一个视图,布局,船长或部分,如果它发现它,然后返回它,如果它没有找到它,我希望它默认为A公司的意见作为默认值。显然,A公司只会看在它自己的文件夹中。
I'm looking at implementing a custom RazorViewEngine
. Basically I have two sites with effectively the same code base. The differences being that they look different. I want to override the standard view engine to make MVC look in two separate locations for it's views, layouts, etc. one for company A and another for Company B. Company A will contain the master views and company B's view will override these masters. So I want the View Engine to look in location B for a view, layout, master or partial if it finds it then return it, if it doesn't find it I want it to default to company A's views as the default. Obviously company A will only look in it's own folder.
确定将问题的症结所在:
我发现这个网站: HTTP://www.aspnetwiki。 COM / MVC-3剃须刀:延长-的视图引擎
Ok to the crux of the question: I've found this site: http://www.aspnetwiki.com/mvc-3-razor:extending-the-view-engine
第一个问题,这是实现这一目标的最佳途径?
First question, is this the best way to achieve this?
的其次,我需要重写 CreatePartial
, CreateView的
, FindPartial
和 FindView
的方法呢?的
Second do I need to override the CreatePartial
, CreateView
, FindPartial
and FindView
methods?
更新
好吧,我已经想通了自己的方法我想重写第二个问题是 CreateView的
和 CreatePartialView
因为在这一点上它的内置视图字符串,我可以用它拨弄。
Ok I've figured out the second question myself, the Methods I want to override are CreateView
and CreatePartialView
as at this point it's built the view string and I can fiddle with it.
推荐答案
确定,在我选择了这里详述的方法结束:<一href=\"http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx\" rel=\"nofollow\">http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx
Ok in the end I opted for an approach detailed here: http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx
由于@Adriano的答案和指针,但最终我觉得这个方法适合我的需要更好。下面的方法可以让我保持标准的功能,但创建一个新的更高的优先级视图位置进行搜索。
thanks to @Adriano for the answers and pointers but in the end I think this approach fits my needs better. The approach below allows me to keep the standard functionality but to create a new higher priority view location to be searched.
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注册
then register in 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());
}
这篇关于我如何实现一个自定义RazorViewEngine找到意见非标准位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!