asp.net-mvc App_Web_*.dll 中的 System.NullReferenceException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26491175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:18:00  来源:igfitidea点击:

System.NullReferenceException in App_Web_*.dll

asp.net-mvcrazornullreferenceexception

提问by MaylorTaylor

I am having an odd issue.

我有一个奇怪的问题。

My MVC application seems to be working perfectly fine except for one view page.

除了一个视图页面外,我的 MVC 应用程序似乎工作得很好。

The view page in question (Organization/Edit) gets a 'NullReferenceException' on every code item on the page. Whether it is Html.TextBoxFor()or HTML.AntiForgeryToken().

有问题的视图页面(组织/编辑)在页面上的每个代码项上都得到一个“NullReferenceException”。无论是Html.TextBoxFor()还是HTML.AntiForgeryToken()

I have my model, view, and controller laid out here on another question that i think is related -- https://stackoverflow.com/questions/26475866/dropdownlistfor-null-reference-error

我在另一个我认为相关的问题上列出了我的模型、视图和控制器——https: //stackoverflow.com/questions/26475866/dropdownlistfor-null-reference-error

As you can see below, my modeldoes have information inside of it. This screen capture was taken at the "Return View("Edit", model)" inside the controller.

正如你在下面看到的,我的模型里面确实有信息。此屏幕截图是在控制器内的“返回视图(“编辑”,模型)”处拍摄的。

Exception Details

异常详情

- Source = App_Web_zu4jlld0
- StackTrace =    at ASP._Page_Views_Organization_Edit_vbhtml.Execute() in C:\Users\mtaylor\Projects\Check Im Here\mtaylor-branch\CheckImHere_v2\CheckImHereMVC\Views\Organization\Edit.vbhtml:line 16
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)

enter image description here

在此处输入图片说明

View

看法

@ModelType CheckImHereMVC.OrganizationEditViewModel

@Using Html.BeginForm("Edit", "Organization", FormMethod.Post)
 @Html.AntiForgeryToken() 'get errors here
 @Html.ValidationSummary(True) 'get errors here
 @Html.TextBoxFor(Function(model) model.organizationSub.subName, New With {.class = "span12"}) 'and errors here
End Using

One thing i notice is that if i comment out my 'textboxfor', my error will occur at the 'ValidationSummary()', if i comment out my 'ValidationSummary()', then my error will occur at 'AntiForgeryToken()'.

我注意到的一件事是,如果我注释掉我的“textboxfor”,我的错误将出现在“ValidationSummary()”处,如果我注释掉我的“ValidationSummary()”,那么我的错误将出现在“AntiForgeryToken()”处。

So it seems that the error just happens at the last possible code area.

所以似乎错误只发生在最后一个可能的代码区域。

回答by MaylorTaylor

I found the answer to my problem here

我在这里找到了我的问题的答案

For anyone finding this:

对于任何发现这个的人:

Try commenting out the next code line AFTER the error.

尝试在错误后注释掉下一行代码。

@ModelType CheckImHereMVC.OrganizationEditViewModel

@Using Html.BeginForm("Edit", "Organization", FormMethod.Post)
   @Html.AntiForgeryToken() 
   @Html.ValidationSummary(True) 
   @Html.TextBoxFor(Function(model) model.organizationSub.subName, New With {.class = "span12"})
   @Html.TextBoxFor(Function(model) model.organizationSub.subTitle, New With {.class = "span12"})
   <img src="@Url.Content(Model.img.imgPath)" alt="IMAGES"/> 'commenting out this line fixed my issue
End Using

In the case above, i would get errors on the model.organizationSub.subTitle. If i commented that line out, i would get errors on the model.organizationSub.subNameline. I then found the link mentioned and commented out the line AFTERall of my TextBoxFors. That fixed my issue.

在上述情况下,我会在model.organizationSub.subTitle. 如果我将该行注释掉,我会model.organizationSub.subName在行中出错。然后我找到了提到的链接,并在我所有的 TextBoxFors之后注释掉了这一行。那解决了我的问题。

From link: "Some times compiler could not point on exact lines having specific kind of errors in razor view may be because it could not keep their line number in stack trace or somewhere. I have found this case with Null Reference Exception and when null is passed in Url.Content.

来自链接:“有时编译器无法指出在剃刀视图中具有特定类型错误的确切行可能是因为它无法将它们的行号保留在堆栈跟踪或某处。我发现这种情况下有空引用异常,当 null 是在 Url.Content 中传递。

So it helps to check the next C# statement in razor view when you did not get any error on the line shown by stack trace."

因此,当您在堆栈跟踪显示的行上没有收到任何错误时,它有助于检查 razor 视图中的下一个 C# 语句。”