关闭捆绑和缩小(WEBFORMS)

时间:2014-06-17 23:37:35

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

我创建了一个新的asp.net网站来测试这个功能。现在我希望能够动态控制是否可以在运行时切换捆绑/缩小。大多数代码是自生成的,如下所示,除了关闭优化的行。

 public static void RegisterBundles(BundleCollection bundles)
        {

            // I ADDED THIS LINE ONLY TO TURN OFF OPTIMIZATION.
            System.Web.Optimization.BundleTable.EnableOptimizations = false;

            bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include(
                            "~/Scripts/WebForms/WebForms.js",
                            "~/Scripts/WebForms/WebUIValidation.js",
                            "~/Scripts/WebForms/MenuStandards.js",
                            "~/Scripts/WebForms/Focus.js",
                            "~/Scripts/WebForms/GridView.js",
                            "~/Scripts/WebForms/DetailsView.js",
                            "~/Scripts/WebForms/TreeView.js",
                            "~/Scripts/WebForms/WebParts.js"));

            // Order is very important for these files to work, they have explicit dependencies
            bundles.Add(new ScriptBundle("~/bundles/MsAjaxJs").Include(
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjax.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js"));

            // Use the Development version of Modernizr to develop with and learn from. Then, when you’re
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                            "~/Scripts/modernizr-*"));

            ScriptManager.ScriptResourceMapping.AddDefinition(
                "respond",
                new ScriptResourceDefinition
                {
                    Path = "~/Scripts/respond.min.js",
                    DebugPath = "~/Scripts/respond.js",
                });
        }

我的问题如下:

1)我认为enableOptimization应该关闭优化,我会在浏览器中看到非捆绑脚本,而不必修改我的标记。我认为scriptManager会自动处理这个并在看到上面提到的标志时推出单个JS文件。

2)在web.config中将调试设置为true也会触发enableoptimization为false。

这些假设都是假的吗?

修改

1)Thisthis是类似的问题,但没有答案!

编辑2

public class OptimizationModule : IHttpModule
{

    private const string PARAM_NAME = "minify";


    public void Dispose()
    {
        //throw new NotImplementedException();
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest +=(new EventHandler(this.context_BeginRequest));
    }



    private void context_BeginRequest(Object source, EventArgs e)
    {
        // Create HttpApplication and HttpContext objects to access
        // request and response properties.

        HttpApplication application = (HttpApplication)source;         

        if (application != null && application.Request.QueryString[PARAM_NAME] != null)
        {
            bool minify;
            bool.TryParse(application.Request.QueryString[PARAM_NAME], out minify);

            if (!minify)
            {
                BundleTable.Bundles.Clear();
                BundleConfigs.GenerateBundles(BundleTable.Bundles, false);                    
            }
            else {
                BundleTable.Bundles.Clear();
                BundleConfigs.GenerateBundles(BundleTable.Bundles, true);
            }

        }
    }


}

我尝试添加httphandler,以便在运行应用程序时更改优化。所以我想我会传递这样的东西:localhost / page.aspx?minify = false在客户端只是为了查看未分类的版本是什么样的,如果我需要调试一些东西。但这似乎没有改变任何东西。这是否意味着我在开始时所拥有的任何设置都将在整个会话期间继续进行,或者可以进行更改。

0 个答案:

没有答案