簡體   English   中英

MVC5 對象引用未設置為 Scripts.Render 上的對象實例

[英]MVC5 Object reference not set to an instance of an object on Scripts.Render

我正在使用 Asp.net MVC5,自從使用 Microsoft MVC 平台 4 年以來,還沒有遇到過這種模棱兩可的錯誤! 錯誤發生在默認 MVC5 模板中的管理頁面中。 _layout.cshtml文件的第 26 行

Line 24:     <![endif]-->
Line 25: 
Line 26:     @Scripts.Render("~/js/intf")
Line 27:     <script>
Line 28:         if (document.documentMode === 10) {

堆棧跟蹤說:

[NullReferenceException:未將對象引用設置為對象的實例。] ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\\Users\\fake\\Documents\\Visual Studio 2013\\Projects\\name\\name\\Views\\Shared_Layout.cshtml:26
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +198
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +104
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +90
System.Web.WebPages.<>c__DisplayClass7.b__6(TextWriter writer) +232 System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +10 System.Web.WebPages.WebPageBase.Write(HelperResult 結果) +80
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action 1 body) +63
System.Web.WebPages.WebPageBase.PopContext() +267
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +98
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 body) +63
System.Web.WebPages.WebPageBase.PopContext() +267
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +98
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 body) +63
System.Web.WebPages.WebPageBase.PopContext() +267
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +98
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1個過濾器,Int32 filterIndex,ResultExecutingContext preContext,ControllerContext controllerContext,ActionResult actionResult) +56

System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList 1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1 filters, ActionResult actionResult) +52

System.Web.Mvc.Async.<>c__DisplayClass28.b__19() +173 System.Web.Mvc.Async.<>c__DisplayClass1e.b__1b(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49

System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +54

System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, 控制器控制器) +12
System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +54

System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +54

System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 結果) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9688704 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&completedSynchronously) +155

編輯:正如你想要的捆綁代碼,我把其中之一放在這里:

bundles.Add(new ScriptBundle("~/js/intf").Include(//first internal scripts
                "~/js/jquery*",
                "~/js/modernizr.js",
                "~/js/ie-prototype-polyfill.js",
                "~/js/prettyPhoto.js"));

注意:如果我評論錯誤行(樣式/腳本捆綁行),錯誤將是@ViewBag.Title作為新錯誤! 我認為 Razor 視圖引擎在這里遇到了麻煩,因為它無法實例化視圖的代碼要求,但是為什么呢?

我知道使用 Microsoft 技術時出現的一些錯誤與它所說的錯誤並沒有真正的關系。 請留下您的有益經驗。 謝謝。

更新~/views/Account/Manage.cshtml文件的簡潔內容可能有用:

@using Social.Models;
@using Microsoft.AspNet.Identity;

@{
    ViewBag.Title = "manage";
}

<div class="layout-app">

    <div class="row row-app margin-none">

        <div class="col-md-12">

            @if (ViewBag.HasLocalPassword)
            {
                 @Html.Partial("_ChangePasswordPartial")
            }
            else
            {
                 @Html.Partial("_SetPasswordPartial")
            }
        </div>
    </div>
</div>

我在主布局文件中濫用ViewBag以在其他頁面中使用。 我的結構將ViewBag作為必填字段,所以我必須在之前實例化它。 但是我忘記實例化它導致觸發這樣的錯誤,它似乎與Scripts.Render無關。

例如,嘗試在 MVC 解決方案中的文件夾 Scripts 中創建一個新的 javascript 文件,並調用文件 app.js(或其他任何文件)。

然后將以下行放在其他 @scripts.Render 行下方以運行它。

<script src="@Scripts.Url("~/Scripts/app.js")"></script>

否則將文件添加到包中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM