0

I know it is possible to use T4MVC to generate a URL based on a specific action using UrlHelper(). See SO and here. However I ultimately need to do this during Application_Start() due to ABP requirements.

So in essence within Application_Start() I have:

  //Areas
  AreaRegistration.RegisterAllAreas();

  //Routes
  RouteConfig.RegisterRoutes(RouteTable.Routes);

  //Get the URL
  var urlHelper = new UrlHelper();
  var url =  urlHelper.ActionAbsolute(MVC.Home.Index())

however this blows up with the following exception:

'UrlHelper.ActionAbsolute(MVC.Home.Index())' threw an exception of type 'System.NullReferenceException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2147467261
    HelpLink: null
    InnerException: null
    Message: "Object reference not set to an instance of an object."
    Source: "T4MVCExtensions"
    StackTrace: "   at System.Web.Mvc.T4Extensions.ActionAbsolute(UrlHelper urlHelper, ActionResult result)"
    TargetSite: {System.String ActionAbsolute(System.Web.Mvc.UrlHelper, System.Web.Mvc.ActionResult)}

however if I inspect the contents of things (at runtime) they appear to all be populated:

MVC.Home.Index()

{T4MVC_System_Web_Mvc_ActionResult}
    Action: "Index"
    Controller: "Home"
    Protocol: null
    RouteValueDictionary: {System.Web.Routing.RouteValueDictionary}

MVC.Home.Index().GetRouteValueDictionary()

{System.Web.Routing.RouteValueDictionary}
    Count: 3
    Keys: Count = 3
    Values: Count = 3
    Results View: Expanding the Results View will enumerate the IEnumerable

So can anyone shed any light on why this is blowing up? Or put another way how do I get the absolute url for an action using T4MVC inside Application_Start()?

Community
  • 1
  • 1
TheEdge
  • 9,291
  • 15
  • 67
  • 135

1 Answers1

0

The common theme between the links you provided were that those calls were made in an action which means that a request has already been materialized. Meaning that a context already exists. At startup there are no requests as yet. That is why you are getting the error.

Nkosi
  • 235,767
  • 35
  • 427
  • 472