我正在使用Tom DuPonts MvcBundler,按照他的指南:http://www.tomdupont.net/2012/03/configuring-bundles-in-mvc-4.html
我在配置文件中有这个
<add bundlePath="~/js/shared" minify="false">
<directories>
<add directoryPath="~/Scripts/Shared" searchPattern="*.js"></add>
</directories>
</add>
这会抛出此错误
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 59: @Html.JsBundle("~/js/types")
Line 60: @Html.JsBundle("~/js/libraries")
Line 61: @Html.JsBundle("~/js/shared") <-------- Offending line
删除
minify="false"
或更新到
minify="true"
允许应用程序正常工作。
有没有办法不使用此扩展名对一组文件进行缩小?
完整堆栈跟踪
[NullReferenceException: Object reference not set to an instance of an object.]
System.Object.GetType() +0
System.Web.Optimization.DefaultBundleBuilder.BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable`1 files) +381
System.Web.Optimization.Bundle.GenerateBundleResponse(BundleContext context) +282
System.Web.Optimization.Bundle.GetBundleUrl(BundleContext context, Boolean includeContentHash) +66
System.Web.Mvc.HtmlHelperExtensions.ReferenceBundle(HtmlHelper helper, String bundlePath, TagBuilder baseTag, String key) +142
ASP._Page_Views_Shared_Layouts_Default__Layout_cshtml.Execute() in Web\Views\Shared\Layouts\Default\_Layout.cshtml:61
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +196
System.Web.WebPages.WebPageBase.Write(HelperResult result) +89
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +233
System.Web.WebPages.WebPageBase.PopContext() +291
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +380
System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +33
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +263
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
编辑:
将库更新到2.2.0后,我现在收到此错误
Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper<dynamic>'
does not contain a definition for 'CssBundle' and no extension method
'CssBundle' accepting a first argument of type
'System.Web.Mvc.HtmlHelper<dynamic>' could be found (are you missing a
using directive or an assembly reference?)
Line 19: <link rel="icon" href="/Content/images/favicon.ico">
Line 20:
Line 21: @Html.CssBundle("~/css/bootstrap")
答案 0 :(得分:1)
这应该在v2.2.0(https://www.nuget.org/packages/MvcBundleConfig/2.2.0)
中修复仅供参考:这个包很旧,需要更新。我将尽快发布3.0版,增加对Bundle Transformer的支持。 (https://bundletransformer.codeplex.com/)
private static void AddBundleConfiguration<T>(BundleCollection bundles, IEnumerable<BundleConfig> bundleConfigs)
where T : IBundleTransform, new()
{
foreach (var bundleConfig in bundleConfigs)
{
// The bug WAS here; it was passing in null instead of an empty array, thus the
// Bundle constructor params would have an array of one that contained a null.
var transform = bundleConfig.Minify
? new IBundleTransform[] { new T() }
: new IBundleTransform[0];
var bundle = String.IsNullOrWhiteSpace(bundleConfig.CdnPath)
? new Bundle(bundleConfig.BundlePath, transform)
: new Bundle(bundleConfig.BundlePath, bundleConfig.CdnPath, transform);