c# - MVC4 捆绑(基于设备类型)

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

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

最佳答案

显然,您无法访问 App_Start 中的请求,因为此时尚未向您的应用发出任何请求。 BundleConfig.cs 仅声明哪些 bundle 可用,您应该在 View 中选择正确的 bundle 。

您可以查看 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 -->

关于c# - MVC4 捆绑(基于设备类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13240169/

相关文章:

asp.net-mvc - 确保您的 Repository 和 UnitOfWork 类共享相同的休眠 session 对象的正确方法是什么?

asp.net-mvc-4 - 将模型从 View 传递到 Controller

css - 回发后 webgrid 上的列宽丢失

c# - 使用反射查找通用方法时发现模糊匹配

c# - Azure Active Directory 使用错误的应用程序客户端 ID

c# - 两种型号之间的差异

c# - ASP.NET Core RedirectToAction 没有退出功能?

c# - 从资源文件中获取值

c# - 为什么允许我们在条件运算符的 else 子句中使用由 "is"表达式定义的参数?

c# - 如何绕过 Marshal.Copy(32 位)长度限制?