简体   繁体   中英

System.Web.Mvc.UrlHelper.Action Throwing ArgumentNullException inserting into RouteCollection

I'm getting the following error on a Html.Action() call and can't figure out why.

Here's the call itself from my Views/home/Index.cshtml file:

<a id="btn-spin-login" href="@Url.Action("Index", "Account")" class="sprite">Login</a>

I'm getting the following error on the error stream. This only seem to happen after the app pool recycles at the preset time. If I go in to recycle it again the error goes away, which makes it harder to debug.

One caveat is that I am putting my controllers in the same folder as the Views, not in the Controllers folder.


System.ArgumentNullException: Value cannot be null.
Parameter name: item
   at System.Web.Routing.RouteCollection.InsertItem(Int32 index, RouteBase item)
   at System.Web.Mvc.RouteCollectionExtensions.FilterRouteCollectionByArea(RouteCollection routes, String areaName, Boolean& usingAreas)
   at System.Web.Mvc.RouteCollectionExtensions.GetVirtualPathForArea(RouteCollection routes, RequestContext requestContext, String name, RouteValueDictionary values, Boolean& usingAreas)
   at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
   at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues)
   at System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName)
   at ASP._Page_Views_Home_Index_cshtml.Execute() in d:\www\live.wof.com\Sony.Wof\Views\home\Index.cshtml:line 54
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.b__17()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.b__22(IAsyncResult asyncResult)

The problem is that your RouteCollection (RouteTable.Routes) has null records, the RouteCollection by default is not thread safe, and in my case this problem occurred when I registered new routes in many threads. Now I'm using the RouteCollection.GetWriteLock to have it be thread safe and now my problem is resolved.

You need to mix single and double quotes. Change the double quotes around @Url.Action to single quotes and it should work.

<a id="btn-spin-login" href='@Url.Action("Index", "Account")' class="sprite">Login</a>

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