Request is not available in this context -> In Glo

2019-05-10 16:51发布

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

2条回答
倾城 Initia
2楼-- · 2019-05-10 17:13

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.

查看更多
不美不萌又怎样
3楼-- · 2019-05-10 17:18

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("\\", "/"); 
查看更多
登录 后发表回答