【发布时间】:2017-07-06 09:51:47
【问题描述】:
- MVC 5
我需要在 Application_BeginRequest() 中调用 UrlHelper 以在应用程序的其他位置设置一些 URL 值。但是,当我这样称呼它时:
var urlHelper = new UrlHelper();
urlHelper.Action(MVC.Bands.Index())
(我使用的是T4MVC,所以这就是 MVC.Bands.Index() 部分的来源并最终返回一个 ActionResult)。我得到以下异常:
'urlHelper.Action(MVC.Sur.Maintenance.Index())' threw an exception of type 'System.ArgumentNullException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147467261
HelpLink: null
InnerException: null
Message: "Value cannot be null.\r\nParameter name: routeCollection"
ParamName: "routeCollection"
Source: "System.Web.Mvc"
StackTrace: " at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)\r\n at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)\r\n at System.Web.Mvc.UrlHelper.RouteUrl(String routeName, RouteValueDictionary routeValues, String protocol, String hostName)\r\n at System.Web.Mvc.T4Extensions.Action(UrlHelper urlHelper, ActionResult result, String protocol, String hostName)\r\n at System.Web.Mvc.T4Extensions.Action(UrlHelper urlHelper, ActionResult result)"
TargetSite: {System.String GenerateUrl(System.String, System.String, System.String, System.Web.Routing.RouteValueDictionary, System.Web.Routing.RouteCollection, System.Web.Routing.RequestContext, Boolean)}
为什么 routeCollection 为 NULL?我本来希望它在我们处理请求时已经启动。
【问题讨论】:
标签: asp.net-mvc t4mvc