Get URL to website from within Application_Start?

六眼飞鱼酱① 提交于 2020-01-21 07:12:51

问题


Hi,

I need to generate a correct URL to a action in my APS.NET MVC 3 website and this have to be done in the Application_Start in Gloabl.asax.

I have tried to set a UrlHelper but the HttpContext.Current is always null?

The adress i look for is somthing like this : http://www.mysite.se/MyController/MyAction.

I have also tried VirtualPathUtility.ToAbsolute("~/") but that will only give me "/mysite/"


回答1:


I'm afraid you can't access the HttpContext in the Application_Start when the application is running in the IIS integration mode (you can achieve that in the classic mode but that's not recommended).

Mike describes here a workaround how to do that in Application_BeginRequest such that your code still runs only for the first request not for the later ones.



来源:https://stackoverflow.com/questions/11458567/get-url-to-website-from-within-application-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!