Request is not available in this context -> In Global.asax -> what is replace

℡╲_俬逩灬. 提交于 2019-12-07 12:08:30

问题


why the below line has error in global.asax :

string RelativeFilePath = "~/" + (AbsoluteFilePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty)).Replace("\\", "/");  

Error :

Request is not available in this context

what is the replacement?

thanks in advance


回答1:


If you are hosting your application in IIS7 integrated pipeline HttpContext objects are not available in Application_Start. For your scenario you could do this instead:

string relativeFilePath = "~/" + AbsoluteFilePath
    .Replace(HostingEnvironment.ApplicationPhysicalPath, String.Empty)
    .Replace("\\", "/"); 



回答2:


In IIS7 or greater, the Integrated pipeline was introduced, and some rules changed. You cannot access the current HttpContext in Application_Start. Here's more information.

To quote, here's your options:

So, what does this mean to you?

Basically, if you happen to be accessing the request context in Application_Start, you have two choices:

Change your application code to not use the request context (recommended). Move the application to Classic mode (NOT recommended).

Since you're just getting the application's physical path, I'd stick with Integrated Mode and just change your code.



来源:https://stackoverflow.com/questions/7757524/request-is-not-available-in-this-context-in-global-asax-what-is-replace

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