Need UrlHelper inside global.asax
I need to set up a bunch of paths and URLs when my app starts, ie in global.asax
. Using UrlHelper.Content()
is the easiest way to do that, but it is not available for use.
So how can I get an instance of it inside global.asax
? I tried creating one, but of course RequestContext
doesn't exist.
The answer most linked-to is h开发者_Python百科ere but that throws exceptions for me. I am using the latest MVC3.
The code that you have linked to doesn't work for you because you are probably running your application in IIS 7 Integrated Mode (in contrast to Classic Mode). In integrated mode you have no access to the HttpContext in Application_Start
. So if on all price you need to generate urls in your Application_Start
method forget about UrlHelper
, you will need some other method. This being said you could of course use this code in events like Application_BeginRequest
, ...
And by the way it's strange that you need to generate urls there. I have never had such need. Maybe you could explain your scenario in more details in order to get a more adapted solution to it.
精彩评论