【问题标题】:MVC4 Bundling (Based on Device Type)MVC4 捆绑(基于设备类型)
【发布时间】:2012-11-05 20:51:21
【问题描述】:

所以查看 bundleconfig.cs 它应该允许根据设备类型进行捆绑。唯一的问题是因为它在 App_Start 中,它不允许我访问 Request 对象。任何想法如何使基于设备的捆绑成为可能?

【问题讨论】:

  • 好问题。我们当然不希望向移动设备发送不必要的 JavaScript 和图像文件。

标签: c# asp.net-mvc asp.net-mvc-4 bundling-and-minification


【解决方案1】:

显然,您无法访问 App_Start 中的请求,因为此时您的应用程序没有收到任何请求。 BundleConfig.cs 仅声明哪些捆绑包可用,您应该在视图中选择正确的捆绑包。

可以看this MVC 4 tutorial的示例代码:

BundleMobileConfig.cs

public class BundleMobileConfig {
    public static void RegisterBundles(BundleCollection bundles) {
        bundles.Add(new ScriptBundle("~/bundles/jquerymobile").
            Include("~/Scripts/jquery.mobile-{version}.js"));

        bundles.Add(new StyleBundle("~/Content/Mobile/css").
            Include("~/Content/Site.Mobile.css"));

        bundles.Add(new StyleBundle("~/Content/jquerymobile/css").
            Include("~/Content/jquery.mobile-{version}.css"));
    }
}

_Layout.Mobile.cshtml

<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title </title>
    <meta name="viewport" content="width=device-width" />

    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Styles.Render("~/Content/Mobile/css", "~/Content/jquerymobile/css")    
</head>
<!-- etc -->

【讨论】:

  • 这完全正确。另请注意,本文在标记为浏览器特定视图的部分中描述了如何比移动/非移动更具体。您将字符串(也称为显示模式)链接到某些浏览器代理检测逻辑,例如:当浏览器代理字符串包含“iPhone”时,“iPhone”。然后你可以创建_Layout.iPhone.cshtml
  • 我认为这不包括 _Layout.Mobile.cshtml 上的 jQuery Mobile .js 文件。
  • @DOK 它没有。这并不是 OP 的问题,而是如何在每个设备的基础上提供一些捆绑包,而不是如何设置 jQuery Mobile。
【解决方案2】:

BundleConfig 中为您想要支持的所有设备创建捆绑包。然后,在您看来,使用基于从Request.Browser 派生的设备的适当捆绑包。

【讨论】:

    猜你喜欢
    • 2012-06-21
    • 1970-01-01
    • 2013-04-02
    • 2012-07-22
    • 1970-01-01
    • 2012-10-15
    • 2012-06-28
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多