简体   繁体   中英

MVC 5 AspNet Identity - Value cannot be null. Parameter name: source

I am seeing a very sporadic error when users log into an MVC 5 app. It only happens once every 100 or so logins and only happens on a remote web server. When it occurs users cannot login, however, if we recycle the app pool the error goes away and everyone can login again.

Exception information: 
    Exception type: ArgumentNullException 
    Exception message: Value cannot be null.
Parameter name: source
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at ASP._Page_Views_Shared__LoginPartial_cshtml.Execute() in c:\inetpub\wwwroot\appname\Views\Shared\_LoginPartial.cshtml:line 18
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
   at ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\inetpub\wwwroot\appname\Views\Shared\_Layout.cshtml:line 42

According to the above the error occurs in the LoginPartial.cshtml which is from the MVC 5 VS template but I cannot find anything there that would cause this and it doesn't make sense why this happens so sporadically and then recycling the app pool fixes it.

@model POC.Web.Models.LoginViewModel

@{ ViewBag.Title = "Log in"; }

<h2>@ViewBag.Title</h2>
<div class="row">
<div class="col-md-12">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <hr />
                @Html.ValidationSummary(true)
                <div class="form-group">
                    @Html.LabelFor(m => m.UserName, new {  @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.UserName, new { autocomplete = "off", @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.UserName)
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Password)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <div class="checkbox">
                            @Html.CheckBoxFor(m => m.RememberMe)
                            @Html.LabelFor(m => m.RememberMe)
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Log in" class="btn btn-default" />
                    </div>
                </div>                    
            }
        </section>
    </div>
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Found the issue, it was in a different view(LoginPartial) than the above(Login). Data was null in the cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM