覆盖MVC 4.0视图引擎 [英] Overriding MVC 4.0 ViewEngine

查看:128
本文介绍了覆盖MVC 4.0视图引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有在不同的目录不同的意见(NOT领域!),所以,当网址包含特定字符串,该视图引擎会导致不同的目录服务的另一种观点(同名)。

I want to have different views in different directories (NOT AREAS!) so that when the URL contains a certain string , the viewEngine will result serving the another view (same name) from a different directory.

在code是在这里:

 public class CheckinViewEngine : WebFormViewEngine
{
    public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
    {
        ViewEngineResult result;

       var routData = controllerContext.RequestContext.RouteData.Values;

       if (controllerContext.RequestContext.HttpContext.Request.Path.ToUpper().Contains("UP"))
       {
         result=  base.FindView(controllerContext, "Home/Up/" + viewName, masterName, useCache);
       }
       else
       {
          result=  base.FindView(controllerContext, viewName, masterName, useCache);
       }
        return result;
    }    

}

在这种情况下,我检查URL包含UP。

In this case I check that the URL contains "UP".

在Global.asax中,我说:

In Global.asax I've added:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CheckinViewEngine ());

我的问题是这样:在ViewEngineResult.View为null而且,对任何要求,我得到这个方法无限循环的请求

My problems are so: the ViewEngineResult.View is null and also, upon any request I get infinite loop request for this method.

任何想法?

推荐答案

1)我想补充一点,你是想找到意见特别的位置在这里看到:

1) I would add the 'special' location that you are wanting to find views in. See here:

<一个href=\"http://stackoverflow.com/questions/632964/can-i-specify-a-custom-location-to-search-for-views-in-asp-net-mvc\">Can我指定一个自定义位置&QUOT;搜索意见&QUOT;在ASP.NET MVC?

2)然后对要位于该文件夹中,在使用时发现你的'特殊'URL的话的看法,加上他们那里,但与视图中的文件名.UP(或其他)。所以如果是正常Index.cshtml添加一个名为Index.Up.cshtml。 (您可以跳过第一步,如果你有更多的只是在渲染根据你的'特殊'字视图感兴趣。

2) Then for the views that you want to be located in that folder and used when your 'special' url words are found, add them there but with .up (or whatever) in the view's filename. So if it is normally Index.cshtml add one called Index.Up.cshtml. (You can skip the first step if you are more just interested in rendering a view based on your 'special' word.

3)然后在你的FindView方法,做测试的URL,如果找到,重命名视图名称为.UP版本,以便视图引擎可以找到它。

3) Then in your FindView method, do your test for the url and if found, rename the view name to the .Up version so the view engine can find it.

这篇关于覆盖MVC 4.0视图引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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