ASP.NET MVC 1 的主题支持,获取视图大师名称
目前我正在开发一个较旧的 ASP.NET MVC 1 应用程序,以添加主题支持。我浏览了网络并能够构建自己的 ViewEngine,到目前为止它运行得很好。只有一个问题是敲打我的。
我已经为 WebFormViewEngine 重写了以下方法:
public override ViewEngineResult FindView(
ControllerContext controllerContext,
string viewName,
string masterName,
bool useCache)
在该方法中,我正在设置主题支持的位置格式。不幸的是,masterName 参数始终为空!所以我需要
if (string.IsNullOrEmpty(masterName))
masterName = "Site";
经常亲自检查以使发动机正常工作。但由于我有几个主文件,一旦视图需要另一个主文件而不是“站点”,这个解决方案就很糟糕。 有谁知道,我如何在此方法中获取主视图名称?
currently I'm developing on an older ASP.NET MVC 1 Application, to add theme support. I've looked around the web and was able to build my own ViewEngine which works quit nice so far. Only one problem is banging my had.
I've overwritten following method for WebFormViewEngine:
public override ViewEngineResult FindView(
ControllerContext controllerContext,
string viewName,
string masterName,
bool useCache)
In this method I'm setting up the location formats for the theme support. Unfortunately the masterName Parameter is always empty! So I need to check
if (string.IsNullOrEmpty(masterName))
masterName = "Site";
always by myself to get the engine working. But as I've several master files, this solution sucks, as soon as a view requires another master than "Site".
Does anybody know, how I can get the master views name in this Method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自己解决了。经过大量研究,我发现了以下片段,这对我有帮助:
必须子类化 WebViewForm 并在 PreInit 事件中处理主文件。
Solved it by myself. After a lot of researching, I've found following snippet, which helped me:
Had to subclass the WebViewForm and handeling the master File in the PreInit event.
解决了类似的问题,但方法略有不同。
假设您在 Theme 文件夹中有替代视图树,那么您必须在从 WebFormViewEngine 派生的类 MyViewEngine 中进行设置:
并覆盖方法:
在 Global.asax.cs 文件中的 Application_Start 方法中添加:
Solved kind of the same problem but the approach was little different.
Suppose you have alternative views tree in Theme folder then you have to set in your class MyViewEngine derived from WebFormViewEngine:
and override method:
In Application_Start method from Global.asax.cs file add:
或者,您可能可以使用我在这个答案中描述的一些技术:如何更改 asp.net mvc 2 中的主题
它适用于 MVC3 和 Razor,但除 View 之外的所有内容都应该在 MVC 1 上正常工作。
Alternatively, you could probably use some of the techniques I've described in this answer: how to change the themes in asp.net mvc 2
It's on MVC3 and Razor, but everything except the View should work just fine on MVC 1 as well.