gpt4 book ai didi

c# - MVC5 对象引用未设置为 Scripts.Render 上的对象实例

转载 作者:可可西里 更新时间:2023-11-01 08:28:50 28 4
gpt4 key购买 nike

我使用的是 Asp.net MVC5,自从使用 Microsoft MVC 平台 4 年以来还没有遇到过这种模棱两可的错误!错误发生在默认 MVC5 模板的 Manage 页面中。这里是 第 26 行,位于 _layout.cshtml文件:

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

堆栈跟踪说:

[NullReferenceException: Object reference not set to an instance of an object.] 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 result) +80
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action1 body) +63<br/>
System.Web.WebPages.WebPageBase.PopContext() +267<br/>
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext
pageContext, TextWriter writer, WebPageRenderingBase startPage) +98<br/>
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,
TextWriter writer, Object instance) +235<br/>
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext
viewContext, TextWriter writer) +107<br/>
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
+291 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext
controllerContext, ActionResult actionResult) +13<br/>
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1
filters, Int32 filterIndex, ResultExecutingContext preContext,
ControllerContext controllerContext, ActionResult actionResult) +420<br/>
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.WrappedAsyncResult1.CallEndDelegate(IAsyncResult
asyncResult) +10<br/>
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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult
asyncResult) +36<br/>
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +54
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult
asyncResult) +28<br/>
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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult
asyncResult) +36<br/>
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 result) +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 View 引擎在这里遇到了麻烦,因为它无法实例化 View 的代码要求,但为什么呢??

我知道在使用 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无关。

关于c# - MVC5 对象引用未设置为 Scripts.Render 上的对象实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29492101/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com