0

I have the following code in my view:

@if (Request.IsAuthenticated)
                            {
   using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm"}))
   {
      @Html.AntiForgeryToken()
      <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
    }
 }

When rendering the page, it's throwing NullReferenceException, but I'm unable to figure out what's causing this. Stacktrace shows that exception is happening in this peace of code:

en ASP._Page_Views_Home_Index_cshtml.Execute() en C:\Proyectos\SDC_Web_Sistema_Documental_Centralizado\OpenText\Views\Home\Index.cshtml:línea 60 en System.Web.WebPages.WebPageBase.ExecutePageHierarchy() en System.Web.Mvc.WebViewPage.ExecutePageHierarchy() en System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) en System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) en System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) en System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) en System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) en System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) en System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

Any help will be appreciated.

Igor
  • 55,253
  • 10
  • 80
  • 149
Oscar
  • 12,656
  • 7
  • 38
  • 62
  • @Igor No, this is not a duplicate, as long as this is happening in a razor view and no object instantiation is done in this code. That answer doesn't solve my problem – Oscar Oct 14 '16 at 10:15
  • 2
    The code you have shown cannot generate that exception. Its dues to code you have not shown –  Oct 14 '16 at 10:16
  • @StephenMuecke This is where vs is stopping. Here the stacktrace makes it clear, in index.cshtml, line 60: en ASP._Page_Views_Home_Index_cshtml.Execute() en C:\Proyectos\SDC_Web_Sistema_Documental_Centralizado\OpenText\Views\Home\Index.cshtml:línea 60 en System.Web.WebPages.WebPageBase.ExecutePageHierarchy() en System.Web.Mvc.WebViewPage.ExecutePageHierarchy() en System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)... – Oscar Oct 14 '16 at 10:20
  • You need to show the whole view (the code you have shown does not throw an error) –  Oct 14 '16 at 10:21
  • 2
    It is a duplicate. There is even a section in the answer about NRE's in Razor views (which is what your question is). NRE's are almost always generic (99% of the time). I recommend you read the answer and apply the troubleshooting tips to your problem. – Igor Oct 14 '16 at 10:23
  • @StephenMuecke But could it happen that the exception is been thrown somewhere else even if VS stops in this line, and stacktrace points to this line? – Oscar Oct 14 '16 at 10:24
  • Your code keep sensitive data? why don't you copy all html code? – Sandip - Full Stack Developer Oct 14 '16 at 10:24
  • 1
    Yes (and razor does not always report the error line correctly anyway). The question will be closed as a dupe, but you might as well show the view. –  Oct 14 '16 at 10:25

0 Answers0