2012-04-27 59 views
0

我正在为检测到的视图名称添加移动扩展程序。如何实现“级联”ViewEngineResults?

下面是当前的代码:

public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) 
{ 
     string GroupName = System.Configuration.ConfigurationManager.AppSettings["GROUP"]; 

     return base.FindView(controllerContext, viewName + "_" + GroupName, masterName, useCache); 
} 

因此,如果路线去SomeController和应用程序设置说,该组织是“TeamA”(例如),将渲染视图将是Index_TeamA.cshtml

但是,如果没有Index_TeamA.cshtml那么它默认为Index.cshtml,正如人们所期望的那样。

(这是因为有一些由一些球队所要求的看法有微小变化。)

但是现在我要到另一个层次添加到这个移动版本:

public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) 
{ 
     string GroupName = System.Configuration.ConfigurationManager.AppSettings["GROUP"]; 

     if (HttpContext.Current.Request.Browser.IsMobileDevice) 
      GroupName += "_MOB"; 

     return base.FindView(controllerContext, viewName + "_" + GroupName, masterName, useCache); 
} 

然而,问题是,如果找不到Index_TeamA_MOB.cshtml,则默认为Index.cshtml,实际上我希望它默认为Index_TeamA.cshtml

这很清楚为什么发生这种情况,问题是我该如何级联这个实现?因此,它首先检查组版本的移动版本(如果检测到移动浏览器),那么如果没有回退到组版本,那么如果没有,那么获取默认视图?

UPDATE,这里是版本1

编辑:好吧这会导致一个无限循环明显。

public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) 
     { 
      string GroupName = System.Configuration.ConfigurationManager.AppSettings["GROUP"]; 

      if (HttpContext.Current.Request.Browser.IsMobileDevice) 
      { 
       var result = ViewEngines.Engines.FindView(controllerContext, viewName + "_" + GroupName + "_MOB", masterName); 

       if (result != null) 
        return base.FindView(controllerContext, viewName + "_" + GroupName + "_MOB", masterName, useCache); 
      } 

      return base.FindView(controllerContext, viewName + "_" + GroupName, masterName, useCache); 
     } 

回答