什么时候的HttpRequest获得创建? [英] When does HttpRequest get created?

查看:946
本文介绍了什么时候的HttpRequest获得创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVC Web应用程序,我检查Request.IsLocal,看看如果应用程序在我的机器上运行 - 如果是这样,我设置一个全局静态变量,它告诉我的应用程序的其余部分,我在调试模式。

In my MVC web application, I'm checking Request.IsLocal to see if the application is running on my machine--if it is, I set a Global static variable which tells the rest of my application that I am in 'Debug Mode'.

现在的问题是,我不知道什么时候做此项检查。

The problem is, I don't know when to do this check.

我试着做了的global.asax.cs文件的Application_Start()下,是这样的:

I tried to do it in the global.asax.cs file, under Application_Start(), like this:

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

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



麻烦的是,Request对象尚未初始化。我得到一个HttpException它说

The trouble is, the Request object has not been initialized yet. I get a HttpException which says

传入的请求不符合任何路线

The incoming request does not match any route

所以,我的问题是什么时候Request对象得到初始化,并且有一些,我可以为了以后的请求对象就可以运行该检查处理的事件?

So, my question is when does the Request object get initialized, and is there an event of some sort that I could handle in order to run this check after the Request object is ready?

推荐答案

在你的MVC网站的应用程序池纺起来的Application_Start()火灾。它并不真正了解请求对象。因此,即使这是设置一些应用程序范围内的正确的地方,你将无法使用 Request.IsLocal 来做到这一点。你将不得不使用不同的化战略。 @用机器名的贾森的建议是一个很好的选择。

Application_Start() fires when your MVC site's app pool is spun up. It doesn't really know about the "request" object. So even though this is the correct place to set something application-wide, you won't be able to do it with Request.IsLocal. You'll have to use a different stragegy. @Jason's suggestion of using the machine name is a good option.

如果您想检查 Request.IsLocal 为每一个请求,在Global.asax中写方法的处理程序的Application_BeginRequest。请参见获取更多信息。

If you'd like to check Request.IsLocal for every request, write a handler for method for Application_BeginRequest in global.asax. See this for more info.

这篇关于什么时候的HttpRequest获得创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆