When is HttpRequest created?

In my MVC web application, I check Request.IsLocal to check if the application is running on my machine - if so, I set a global static variable that tells the rest of my application that I am in 'Debug mode'.

The problem is that I do not know when to do this check.

I tried to do this in the global.asax.cs file in the Application_Start () section, for example:

protected void Application_Start()
{
    if (Request.IsLocal)
        isDebug = true;

    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}

The problem is that the Request object has not yet been initialized. I get an HttpException that says

Incoming request does not match any route

So my question is, when is the Request object initialized, and is there some kind of event that I could handle to trigger this check after the Request object is ready?

+2
5

MVC _Start(). "". , , - , Request.IsLocal. . @Jason . .

Request.IsLocal , Application_BeginRequest global.asax. . this.

+2

System.Environment.MachineName, , .

+3
+2

bool isLocal = HttpContext.Current.Request.IsLocal;, Application_Start

: ASAX -

0

The request and HttpContext.Current are created for each request (it may also look like a single object, in fact it is not). Therefore, if you want to set the general configuration of the application - Application_Start - this is the right place, but you will not have a request object (even if you are mistaken, since requests do not necessarily come from the same computer all the time).

0
source

Source: https://habr.com/ru/post/1771159/


All Articles