BundleConfig类中的MVC错误:mscorlib.dll中发生类型"System.NullReferenceException"的异常

RAM*_*RAM 6 c# asp.net-mvc bundle runtime-error

我的MVC网站有一个Runtime Error在开始BundleConfig用下面的标题类:

mscorlib.dll中出现"System.NullReferenceException"类型的异常,但未在用户代码中处理

在此输入图像描述

Usings:

using System.Web.Optimization;
using BundleTransformer.Core.Bundles;
using BundleTransformer.Core.Orderers;
using BundleTransformer.Core.Transformers;
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   BundleTransformer.Core.BundleTransformerContext..ctor() +162
   BundleTransformer.Core.BundleTransformerContext.<.cctor>b__0() +44
   System.Lazy`1.CreateValue() +416
   System.Lazy`1.LazyInitValue() +152
   System.Lazy`1.get_Value() +75
   BundleTransformer.Core.BundleTransformerContext.get_Current() +60
   BundleTransformer.Core.Transformers.CssTransformer..ctor(IMinifier minifier, IList`1 translators, IList`1 postProcessors, String[] ignorePatterns) +79
   BundleTransformer.Core.Transformers.CssTransformer..ctor() +97
   NWebsite.BundleConfig.RegisterBundles(BundleCollection bundles) in d:\Documents\Visual Studio 2013\Projects\N\Web\N.Website\App_Start\BundleConfig.cs:18
   NWebsite.MvcApplication.Application_Start() in d:\Documents\Visual Studio 2013\Projects\N\Web\N.Website\Global.asax.cs:20

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9916613
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9930508
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
Run Code Online (Sandbox Code Playgroud)

和Global.asax文件中的MvcApplication类:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}
Run Code Online (Sandbox Code Playgroud)

它是什么原因,我该如何解决?

Iva*_*anL 10

我设法解决了这个问题.当您在web.config中缺少正确的配置时,似乎会发生这种情况(在我的情况下,这是由于给定环境缺少Web配置转换而发生的).因此,请确保web.config中存在以下内容:

<bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd">
<core>
  <css>
    <translators>
      <add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
      <add name="LessTranslator" type="BundleTransformer.Less.Translators.LessTranslator, BundleTransformer.Less" />
    </translators>
    <postProcessors>
      <add name="UrlRewritingCssPostProcessor" type="BundleTransformer.Core.PostProcessors.UrlRewritingCssPostProcessor, BundleTransformer.Core" useInDebugMode="false" />
    </postProcessors>
    <minifiers>
      <add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
    </minifiers>
    <fileExtensions>
      <add fileExtension=".css" assetTypeCode="Css" />
      <add fileExtension=".less" assetTypeCode="Less" />
    </fileExtensions>
  </css>
  <js>
    <translators>
      <add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
    </translators>
    <minifiers>
      <add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
    </minifiers>
    <fileExtensions>
      <add fileExtension=".js" assetTypeCode="JavaScript" />
    </fileExtensions>
  </js>
</core>
<less>
  <jsEngine name="MsieJsEngine" />
</less>
</bundleTransformer>
<jsEngineSwitcher xmlns="http://tempuri.org/JavaScriptEngineSwitcher.Configuration.xsd">
<core>
  <engines>
    <add name="MsieJsEngine" type="JavaScriptEngineSwitcher.Msie.MsieJsEngine, JavaScriptEngineSwitcher.Msie" />
  </engines>
</core>
</jsEngineSwitcher>
Run Code Online (Sandbox Code Playgroud)