尝试创建新的Bundle时,NoTransform无效

时间:2012-06-12 21:00:44

标签: asp.net asp.net-mvc bundle asp.net-optimization

我正在关注一个示例,并且在为MVC项目创建自定义包时无法使用“NoTransform”。这是我的代码无法编译,因为“NoTransform”产生一个错误,上面写着“错误1无法找到类型或命名空间名称'NoTransform'(你是否缺少using指令或程序集引用?)” 。有什么想法吗?

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.WebPages;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace CoyleAzure
{


    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);


            // Added to Add DisplayModes


            DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("Phone")
            {
                ContextCondition = (context => (
                (context.GetOverriddenUserAgent() != null) &&
                (
                (context.GetOverriddenUserAgent().IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >= 0) ||
                (context.GetOverriddenUserAgent().IndexOf("iPod", StringComparison.OrdinalIgnoreCase) >= 0) ||
                    (context.GetOverriddenUserAgent().IndexOf("Droid", StringComparison.OrdinalIgnoreCase) >= 0) ||
                        (context.GetOverriddenUserAgent().IndexOf("Blackberry", StringComparison.OrdinalIgnoreCase) >= 0) ||
                            (context.GetOverriddenUserAgent().StartsWith("Blackberry", StringComparison.OrdinalIgnoreCase))
            )
            ))
            });






            DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("Tablet")
            {
                ContextCondition = (context => (
                    (context.GetOverriddenUserAgent() != null) &&
                    (
                    (context.GetOverriddenUserAgent().IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0) ||
                    (context.GetOverriddenUserAgent().IndexOf("Playbook", StringComparison.OrdinalIgnoreCase) >= 0) ||
                        (context.GetOverriddenUserAgent().IndexOf("Transformer", StringComparison.OrdinalIgnoreCase) >= 0) ||
                            (context.GetOverriddenUserAgent().IndexOf("Xoom", StringComparison.OrdinalIgnoreCase) >= 0)
                        )
                        ))
            });







            IBundleTransform jsTransformer;
            IBundleTransform cssTransformer;

#if DEBUG
            jsTransformer = new NoTransform("text/javascript");
            cssTransformer = new NoTransform("text/css");


#else   
            jsTransformer = new JsMinify();   
            cssTransformer = new CssMinify(); 
#endif

            var
            DesktopJSBundle = new Bundle("~/Scripts/DesktopJS", jsTransformer);

            DesktopJSBundle.Include("~/Scripts/jquery-1.6.4. js");
            DesktopJSBundle.Include("~/Scripts/jquery-ui-1.8.11. js");
            DesktopJSBundle.Include("~/Scripts/jquery.unobtrusive-ajax.js");
            DesktopJSBundle.Include("~/Scripts/jquery.validate.js");
            DesktopJSBundle.Include("~/Scripts/jquery.validate.unobtrusive.js");

            BundleTable.Bundles.Add(DesktopJSBundle);

            var DesktopCSSBundle = new Bundle("~/Content/DesktopCSS", cssTransformer);
            DesktopCSSBundle.Include("~/Content/Site.css");
            BundleTable.Bundles.Add(DesktopCSSBundle);



            var MobileJSBundle = new Bundle("~/Scripts/MobileJS", jsTransformer);
            MobileJSBundle.Include("~/Scripts/jquery-1.6.4.js");
            MobileJSBundle.Include("~/Scripts/jquery.mobile-1.1.0.js");
            BundleTable.Bundles.Add(MobileJSBundle);

            var MobileCSSBundle = new Bundle("~/Content/MobileCSS", cssTransformer);
            MobileCSSBundle.Include("~/Content/jquery.mobile-1.1.0.css");
            MobileCSSBundle.Include("~/Content/jquery.mobile.structure-1.1.0.css");
            BundleTable.Bundles.Add(MobileCSSBundle);
            //BundleTable.Bundles.RegisterTemplateBundles(); 
            BundleTable.Bundles.EnableDefaultBundles();
        }




        //BundleConfig.RegisterBundles(BundleTable.Bundles);




    }
}

4 个答案:

答案 0 :(得分:7)

NoTransform类不再需要公开,因为从RC开始,Transform上的空Bundle类隐含地表示NoTransform

在:

new Bundle("~/yourbundle", new NoTransform())

现在:

new Bundle("~/yourbundle")

我们觉得不需要虚拟实例更干净。我们将其保持在内部,因为它有一点点逻辑可能会导致一些麻烦,因为Transform负责设置响应的contentType。默认逻辑使用捆绑包中第一个文件的文件扩展名(.js / .css)。

此外,通过添加Script / Styles Render助手,您不再需要根据debug = true | false动态切换不同的变换。帮助者应该为你照顾。

答案 1 :(得分:7)

不能直接回答这个问题,只是指出任何遇到这种情况的人......也可以通过配置来完成,(见https://stackoverflow.com/a/12605451/6486

<system.web>
    <compilation debug="true" />
    <!-- Lines removed for clarity. -->
</system.web>

答案 2 :(得分:3)

此代码来自MVC 4 Beta(或更准确地说是System.Web.Optimization beta1),但您可能正在尝试使用MVC 4 RC(或更准确地说是System.Web.Optimization beta2)进行编译(如果MVC 4完全可用) )。在MVC 4中,RC NoTransforminternal

答案 3 :(得分:1)

NoTransform和其他方法(如ScriptsStyles ..)是MVC 4 beta和MVC 4 RC版本中可用的捆绑和缩小的新功能。我希望你没有使用正确版本的ASP.NET MVC,因此你得到了编译错误。

http://blog.kurtschindler.net/post/disabling-bundling-and-minification-in-aspnet-45mvc-4