我正在尝试根据我传入的 slug 切换视图...我可以验证 slug 并且逻辑是正确的,但找不到我的视图:
全球.asax.cs:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new DeviceCapableViewEngine());
DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("mobile")
{
ContextCondition = Context => Context.Request.Browser["IsMobile"] == "True"
&& Context.Request.Browser["IsTablet"] == "False"
});
DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("")
{
ContextCondition = Context => Context.Request.Browser["IsMobile"] == "False"
|| Context.Request.Browser["IsTablet"] == "True"
});
DeviceCapableViewEngine:
public class DeviceCapableViewEngine : FixedRazorViewEngine
{
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
ViewEngineResult result = null;
switch (ThemeContext.GetTheme())
{
case "Blue":
result = base.FindView(controllerContext, string.Format("{0}.{1}", viewName, "blue"), masterName, useCache);
break;
case "Red":
result = base.FindView(controllerContext, string.Format("{0}.{1}", viewName, "red"), masterName, useCache);
break;
}
if (result == null || result.View == null)
result = base.FindView(controllerContext, viewName, masterName, useCache);
return result;
}
我有一个相当标准的设置,我可以确认“蓝色”或“红色”断点被击中。如果它们是,我不希望 MVC 注入“.mobile”......相反,我希望它是“viewName.red”......但似乎使用我的 DDR,它总是会尝试使用碱基调用,这是真的吗?
无论如何,它都会返回“viewName.mobile”。有什么我做错了吗?