更新包后,未将对象引用设置为 _Layout.cshtml 中的对象实例

问题描述 投票:0回答:11

该项目运行良好,直到我通过以下方式更新了所有 NuGet 包 NuGet Manager(我试图获取最新的 OWIN 软件包)。

这个错误在

_Layout.cshtml
,我根本没有碰过它。

未将对象引用设置为对象的实例。

描述:执行过程中发生未处理的异常 当前的网络请求。请查看堆栈跟踪以了解更多信息 有关错误及其在代码中的来源的信息。

异常详细信息:

System.NullReferenceException
:对象引用不正确 设置为对象的实例。

来源错误:

Line 13:  
Line 14:     <!-- Custom Fonts -->
Line 15:     @Scripts.Render("~/Content/fonts")

源文件:f:\Workplace\lrc\lrc\Views\Shared_Layout.cshtml 行: 15


更新

这是

_Layout.cshtml
中的代码(它们是创建项目时自动生成的):

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="LRC - We are aiming at building up a healthier life style.">
    <meta name="author" content="Winston Fan">
    <title>@ViewBag.Title - LRC</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

    <!-- Custom Fonts -->
    @Scripts.Render("~/Content/fonts")
    
    <link href="http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">

所以它在抱怨这行代码:

@Scripts.Render("~/Content/fonts")

但是正如你所看到的,这是VS生成的代码,我怎么会出错呢?

我还检查了

Conntent/font-awesome/fonts
文件夹中的内容,所有需要的资源都在那里。

这是

BundleConfig
内的代码(也是自动生成的代码):

bundles.Add(new ScriptBundle("~/Content/fonts").Include(
              "~/Content/font-awesome/fonts/fontawesome-*",
              "~/Content/font-awesome/fonts/FontAwesome.otf"));

我的

Microsoft.OWin.Security
Microsoft.OWin.Security.Google
Microsoft.OWin
是版本
3.0.1.0


更新2

这是堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an
object.]    Microsoft.Ajax.Utilities.JSParser.ParseExpression(AstNode
leftHandSide, Boolean single, Boolean bCanAssign, JSToken inToken)
+1007    Microsoft.Ajax.Utilities.JSParser.ParseExpressionStatement(Boolean
fSourceElement) +268   
Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean
fSourceElement, Boolean skipImportantComment) +1552   
Microsoft.Ajax.Utilities.JSParser.ParseBlock() +164   
Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean
fSourceElement, Boolean skipImportantComment) +498   
Microsoft.Ajax.Utilities.JSParser.ParseBlock() +164   
Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean
fSourceElement, Boolean skipImportantComment) +498   
Microsoft.Ajax.Utilities.JSParser.ParseBlock() +164   
Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean
fSourceElement, Boolean skipImportantComment) +498   
Microsoft.Ajax.Utilities.JSParser.ParseStatements(Block block) +159   
Microsoft.Ajax.Utilities.JSParser.InternalParse() +1011   
Microsoft.Ajax.Utilities.JSParser.Parse(DocumentContext sourceContext)
+123    Microsoft.Ajax.Utilities.JSParser.Parse(DocumentContext sourceContext, CodeSettings settings) +54   
Microsoft.Ajax.Utilities.Minifier.MinifyJavaScript(String source,
CodeSettings codeSettings) +987   
System.Web.Optimization.JsMinify.Process(BundleContext context,
BundleResponse response) +310   
System.Web.Optimization.Bundle.ApplyTransforms(BundleContext context,
String bundleContent, IEnumerable`1 bundleFiles) +263   
System.Web.Optimization.Bundle.GenerateBundleResponse(BundleContext
context) +355   
System.Web.Optimization.Bundle.GetBundleResponse(BundleContext
context) +104   
System.Web.Optimization.BundleResolver.GetBundleContents(String
virtualPath) +254   
System.Web.Optimization.AssetManager.DeterminePathsToRender(IEnumerable`1
assets) +252   
System.Web.Optimization.AssetManager.RenderExplicit(String tagFormat,
String[] paths) +75   
System.Web.Optimization.Scripts.RenderFormat(String tagFormat,
String[] paths) +292   
System.Web.Optimization.Scripts.Render(String[] paths) +51   
ASP._Page_Views_Shared__Layout_cshtml.Execute() in
f:\Workplace\lrc\lrc\Views\Shared\_Layout.cshtml:15   
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271   
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +121   
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext
pageContext, TextWriter writer, WebPageRenderingBase startPage) +145  
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext
pageContext, TextWriter writer) +41   
System.Web.WebPages.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter
writer) +335    System.Web.WebPages.HelperResult.WriteTo(TextWriter
writer) +42   
System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer,
HelperResult content) +45   
System.Web.WebPages.WebPageBase.Write(HelperResult result) +53   
System.Web.WebPages.WebPageBase.RenderSurrounding(String
partialViewName, Action`1 body) +178   
System.Web.WebPages.WebPageBase.PopContext() +229   
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext
pageContext, TextWriter writer, WebPageRenderingBase startPage) +154  
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,
TextWriter writer, Object instance) +695   
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext
viewContext, TextWriter writer) +382   
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
+431    System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext
controllerContext, ActionResult actionResult) +39   
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1
filters, Int32 filterIndex, ResultExecutingContext preContext,
ControllerContext controllerContext, ActionResult actionResult) +116  
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1
filters, Int32 filterIndex, ResultExecutingContext preContext,
ControllerContext controllerContext, ActionResult actionResult) +529  
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext
controllerContext, IList`1 filters, ActionResult actionResult) +106   
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
+321    System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult
asyncResult) +185   
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult
asyncResult) +42   
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133   
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +56   
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult
asyncResult) +40   
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult
asyncResult, ExecuteCoreState innerState) +34   
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult
asyncResult) +70   
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139   
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +59   
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +40   
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult
asyncResult, Controller controller) +39   
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult
asyncResult) +62   
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139   
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +59   
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +40    System.Web.Mvc.Controller.EndExecute(IAsyncResult
asyncResult) +39   
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult
asyncResult) +39   
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult
asyncResult, ProcessRequestState innerState) +39   
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult
asyncResult) +70   
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139   
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +59   
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +40   
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
+40    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult
result) +38   
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+932    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +188

更新3

这是

Content
文件夹:

enter image description here


更新4

以下是

packages.config
的内容:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.5.0.2" targetFramework="net451" />
  <package id="bootstrap" version="3.3.4" targetFramework="net451" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net451" />
  <package id="jQuery" version="2.1.3" targetFramework="net451" />
  <package id="jQuery.Validation" version="1.13.1" targetFramework="net451" />
  <package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net451" />
  <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net451" />
  <package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net451" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net451" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net451" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Security.Facebook" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Security.Google" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Security.MicrosoftAccount" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Security.Twitter" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
  <package id="Modernizr" version="2.8.3" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
  <package id="Owin" version="1.0" targetFramework="net451" />
  <package id="Respond" version="1.4.2" targetFramework="net451" />
  <package id="WebGrease" version="1.6.0" targetFramework="net451" />
</packages>
c# asp.net-mvc razor asp.net-mvc-5 owin
11个回答
164
投票

这里通过在 BundleConfig 中将“ScriptBundle”替换为“Bundle”来修复。

我们更换了以下内容:

bundles.Add(new ScriptBundle("~/bundles/mybundle").Include(
            "~/Scripts/...",
            "~/Scripts/..."));

与:

bundles.Add(new Bundle("~/bundles/mybundle").Include(
            "~/Scripts/...",
            "~/Scripts/..."));

36
投票

检查 JavaScript 是否有错误。我的是写了一半的行,以“function”结尾


9
投票

这看起来像是 javascript 压缩器中的一个错误。

Microsoft.Ajax.Utilities.Minifier.MinifyJavaScript(String source, CodeSettings codeSettings)
+987

请提交一个连接错误(我也会在 Microsoft 内部转发此线程)。

作为解决方法,您可以手动缩小脚本并直接包含它们,而不是使用捆绑器。

你能分享一下你的packages.config吗?它将有助于跟踪这个潜在的错误。


6
投票

所以我今天早些时候遇到了这个错误,并能够追踪到有问题的代码。对于我自己来说,压缩器被一行糟糕的 javascript 窒息了,该行错误地使用了以下形式的三元语句:

var test = condition ? true_value ? false_value;

显然,第二个问号应该是冒号才能构成有效的三元语句。这导致整个应用程序崩溃并抛出与上述相同的错误。也许这与您遇到的问题不同,但它给出了相同的结果。


1
投票

ScriptBundle
类可用于捆绑脚本,不能用于捆绑样式。它与
@Scripts.Render("~/scripts/bundle")
结合使用,呈现
<script>
标签。

对于样式有

StyleBundle

bundles.Add(new StyleBundle("~/bundles/mybundle").Include(
        "~/Content/...",
        "~/Content/..."));

并且它与

@Styles.Render("~/content/bundle")
结合创建了
<link>
标签。


1
投票

就我而言,这是由于 verdor 库的更改导致的,所以我无法直接修复 js 文件。

我的解决方法是使用

mi
n 版本,该版本向捆绑包指示:请勿缩小此文件。

例如:只需从

select2.js
移动到
select2.min.js


1
投票

一定有 JavaScript 错误。

撤消您的更改,只需逐一检查您的更改,并检查浏览器是否包含这些更改。


1
投票

我读到,更改 BundleConfig.cs 中创建捆绑包的方式可以解决该问题。所以我尝试更换:

bundles.Add(new ScriptBundle(“~/bundles/bootstrap”).Include(“~/Scripts/bootstrap.js”));

bundles.Add(new Bundle(“~/bundles/bootstrap”).Include(“~/Scripts/bootstrap.js”));

通过此更改,项目运行没有异常,显示网页,但看起来没有 CSS 渲染。显示看起来不正常。


0
投票

在我的例子中,捆绑器/压缩器似乎不喜欢以换行符结尾的模板文字。

这有效(没有前导/尾随换行符):

    var t = `<div>
    ...
</div>`;

这有效(前导换行):

    var t = `
<div>
    ...
</div>`;

这有效(前导/尾随换行符用空格缩进):

    var t = `
        <div>
            ...
        </div>
        `; // closing backtick with some whitespace in front

一旦我这样做,@Scripts.Render 就会爆炸:

    var t = `
<div>
    ...
</div>
`; // closing backtick at the start of the line

0
投票

确保在 BundleConfig 中将所有 ScriptBundle 替换为 Bundle


0
投票

我现在也遇到同样的问题。我刚刚通过实验发现它绊倒了 EcmaScript 6 功能(例如,“let”关键字)。 我认为这就是您的情况下 nuget 更新后出现问题的原因。 就我而言,“new ScriptBundle -> new Bundle”也有帮助,谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.