How to disable Javascript/CSS minification in ASP.NET MVC 4 Beta

前端 未结 10 1329
暗喜
暗喜 2020-12-09 10:17

I am just trying out ASP.NET MVC 4 but I can\'t figure out how to disable Javascript/CSS minification feature. Especially for development environment this will help greatly

10条回答
  •  自闭症患者
    2020-12-09 11:06

    Another option would be to create an HTML Helper that you could use to build the script and link tags. Here is what I have implemented for the Javascript, which can also be done for the CSS:

    public static class BundleHelper
        {
            public static MvcHtmlString JsBundle(this HtmlHelper helper, string bundlePath)
            {
                var jsTag = new TagBuilder("script");
                jsTag.MergeAttribute("type", "text/javascript");
    
                return ReferenceBundle(helper, bundlePath, jsTag);
            }
    
            public static MvcHtmlString ReferenceBundle(this HtmlHelper helper, string bundlePath, TagBuilder baseTag)
            {
                var httpContext = helper.ViewContext.HttpContext;
                var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
    
                Bundle bundle = BundleTable.Bundles.GetBundleFor(bundlePath);
                var htmlString = new StringBuilder();
    
                if (bundle != null)
                {
                    var bundleContext = new BundleContext(helper.ViewContext.HttpContext, BundleTable.Bundles, urlHelper.Content(bundlePath));
    
                    if (!httpContext.IsDebuggingEnabled)
                    {
                        baseTag.MergeAttribute("href", System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl(bundlePath));
                        return new MvcHtmlString(baseTag.ToString());
                    }
    
                    foreach (var file in bundle.EnumerateFiles(bundleContext))
                    {
                        var basePath = httpContext.Server.MapPath("~/");
                        if (file.FullName.StartsWith(basePath))
                        {
                            var relPath = urlHelper.Content("~/" + file.FullName.Substring(basePath.Length));
                            baseTag.MergeAttribute("href", relPath, true);
                            htmlString.AppendLine(baseTag.ToString());
                        }
                    }
    
                }
    
                return new MvcHtmlString(htmlString.ToString());
            }
        }
    

    Now all that you have to do is call it in your view:

    
        
        @ViewBag.Title - My ASP.NET MVC Application
        
        
        
        @Html.JsBundle("~/scripts/js")
        
    
    

    And it will render the scripts as separate references, or use the new bundling/minification feature depending on what the debug setting is in your web.config . I used some of the code from http://codecutout.com/resource-minify-bundling as a reference when creating my helper if you wanted to see some more examples. Their helper is written a little better, throwing exceptions when invalid arguments are supplied, etc.... I just haven't gotten around to cleaning mine up yet.

提交回复
热议问题