ASP. NET Request is not available in this context

Source: Internet
Author: User

The previous article complained about the design of ASP. NET FormsAuthentication and the design of HttpContext.

If the ASP. NET program runs in IIS integration mode, in Application_Start () of Global. asax, you only need to access Context. Request, for example, the following code

Var request = Context. Request;

An exception is thrown:

Request is not available in this context

If you don't believe it, try it.

This problem only occurs in the IIS integration mode (Integrated). If it is changed to the traditional mode (Classic), the problem will not occur.

Today, I was overwhelmed by this problem. In the Error Log Module, we add operations to record the current access URL. In this way, when an error occurs, we can accurately know the access URL that causes the error. We added the following

Code:

HttpContext context = HttpContext. Current;
If (context! = Null & context. Request! = Null & context. Request. Url! = Null)
{
Return context. Request. Url. AbsoluteUri;
}

Then, deploy the updated log module to the server. in an application, the "Request is not available in this context" exception occurs, for example:

From the preceding exception information, we can see that the exception occurs when the Request attribute of HttpContext is accessed in Application_Start (the application logs in Application_Start, so it accesses HttpContext. Request ).

Use ILSpy to view

Code:

Internal bool HideRequestResponse;

Public HttpRequest Request
{
Get
{
If (this. HideRequestResponse)
{
Throw new HttpException (SR. GetString ("Request_not_available "));
}
Return this. _ request;
}
}

It can be seen that this exception is thrown when HideRequestResponse = true, that is, in the Application_Start stage, the value of HideRequestResponse is true.

It is confusing that since the Request attribute is not allowed to be accessed when HideRequestResponse = true, Why does HttpContext not provide a way for the caller to know-"Request is prohibited at this time ". If the caller can check the relevant rules before calling the API, the caller does not have to waste his feelings and pay the price (capture this HttpException ).

In addition, we only need to get the URL of the current Request. If we cannot access the Request, we cannot get this URL. Cannot get the URL of the current request in Application_Start? This URL is passed to ASP from external (IIS. NET Runtime, and ASP. NET Runtime status has nothing to do with it...

However, the problem can only be solved first and identified by capturing exceptions,

The Code is as follows:

HttpContext context = HttpContext. Current;
If (context! = Null & context. Request! = Null & context. Request. Url! = Null)
{
Try
{
Return context. Request. Url. AbsoluteUri;
}
Catch (Exception ex)
{
If (ex is HttpException)
{
Return string. Empty;
}
}
}
Author: dudu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.