【问题标题】:.NET MVC DisplayModeProvider fallback.NET MVC DisplayModeProvider 回退
【发布时间】:2019-05-09 18:02:36
【问题描述】:

我目前正在使用DisplayModeProvider 检查是否有移动请求进入,如果我检测到移动请求,则提供Page.mobile.cshtml 文件,否则我将提供默认页面Page.cshtml。这也可以作为后备 - 如果有对 PageX 的移动请求,但 PageX.mobile.cshtml 不存在但有 PageX.cshtml,我将提供 PageX.cshtml。这正在按预期工作。

我想添加回退行为,因为我包括对平板电脑请求的支持。因此,当检测到平板设备请求时,如果我有 Page.tablet.cshtml,它将继续提供该文件。如果没有...tablet.cshtml 文件,我希望它尝试提供Page.mobile.cshtml 文件,如果Page.mobile.cshtml 不存在,我们将提供Page.cshtml 文件。

有没有一种方法可以做到这一点,而不必为每个页面创建一个...tablet.csthml 文件并在其中添加一个Html.Partial...mobile.cshtml

【问题讨论】:

    标签: asp.net-mvc mobile tablet displaymode


    【解决方案1】:

    您可以通过动态更改路线偏好来做到这一点。根据需要定义层次结构,例如首先是平板电脑,然后是移动设备,然后是网页。

    这是 CustomViewEngine 如何做到这一点的示例:

    public class MyViewEngine : RazorViewEngine
    {
        public MyViewEngine()
            : base()
        {
            ViewLocationFormats = new[] {
            "~/Views/tab/{1}/%1/{0}.cshtml",
            "~/Views/mobile/{1}/%1/{0}.cshtml",
            "~/Views/{1}/%1/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };
    
        PartialViewLocationFormats = new[] {
            "~/Views/tab/%1/{1}/{0}.cshtml",
            "~/Views/mobile/%1/{1}/{0}.cshtml",
            "~/Views/%1/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };
       }
    }
    

    这里将首先在 /Views/tab/ 文件夹中搜索视图,然后在 /Views/mobile/ 之后搜索 /Views//Views/Shared/ 文件夹。

    这里已经讨论了实现的细节:ASP.NET MVC Custom View Routing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 2012-02-21
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多