I am in the process of adding a mobile extension to the view names when it is detected.
Here is the current code:
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);
}
So if the route goes to SomeController
and the app settings says the group is "TeamA" (for example) the view that will render will be Index_TeamA.cshtml
.
However if there is no Index_TeamA.cshtml
then it defaults to Index.cshtml as one would expect.
(This is because there are very slight variations in some of the views requested by some teams.)
However now I want to add another level to this for mobile versions:
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);
}
However, the problem is that if Index_TeamA_MOB.cshtml
is not found, it defaults to Index.cshtml
, when I actually want it to default to Index_TeamA.cshtml
.
It's pretty clear why this happens, the question is how can I cascade this implementation? So it first checks for the mobile version of the group version (if mobile browser detected), then if that's not there fallback to the group version, then if that's not there get the default view?
UPDATE, here is version 1
EDIT: Ok this causes an infinite loop apparently.
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);
}