asp.net-mvc 控制器实例上的 HttpContext 在 ASP.net MVC 中为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/223317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 22:48:56  来源:igfitidea点击:

HttpContext on instances of Controllers are null in ASP.net MVC

asp.net-mvchttpcontext

提问by Hugoware

This may not be the correct way to use controllers, but I did notice this problem and hadn't figured out a way to correct it.

这可能不是使用控制器的正确方法,但我确实注意到了这个问题并且还没有想出纠正它的方法。

public JsonResult SomeControllerAction() {

    //The current method has the HttpContext just fine
    bool currentIsNotNull = (this.HttpContext == null); //which is false    

    //creating a new instance of another controller
    SomeOtherController controller = new SomeOtherController();
    bool isNull = (controller.HttpContext == null); // which is true

    //The actual HttpContext is fine in both
    bool notNull = (System.Web.HttpContext.Current == null); // which is false        

}

I've noticed that the HttpContext on a Controller isn't the "actual" HttpContext that you would find in System.Web.HttpContext.Current.

我注意到控制器上的 HttpContext 不是您会在 System.Web.HttpContext.Current 中找到的“实际”HttpContext。

Is there some way to manually populate the HttpContextBase on a Controller? Or a better way to create an instance of a Controller?

有没有办法在控制器上手动填充 HttpContextBase?还是创建控制器实例的更好方法?

采纳答案by Brad Wilson

Controllers are not designed to be created manually like you're doing. It sounds like what you really should be doing is putting whatever reusable logic you have into a helper class instead.

控制器并非设计为像您所做的那样手动创建。听起来您真正应该做的是将您拥有的任何可重用逻辑放入辅助类中。

回答by Hugoware

For now I'm going to do the following. This seems to be an acceptable fix...

现在,我将执行以下操作。这似乎是一个可以接受的修复......

public new HttpContextBase HttpContext {
    get {
        HttpContextWrapper context = 
            new HttpContextWrapper(System.Web.HttpContext.Current);
        return (HttpContextBase)context;                
    }
}

Where this is added to a Controller class these Controllers are inheriting from.

在将其添加到这些控制器继承自的控制器类的地方。

I'm not sure if the HttpContext being null is the desired behavior, but this will fix it in the meantime for me.

我不确定 HttpContext 为 null 是否是所需的行为,但这将同时为我修复它。

回答by Paco

The HttpContext, in the ControllerContext is null because it is not set when the controller is created. The contructor of the controller does not assign this property, so it will be null. Normally, the HttpContext is set to the HttpContext of the ControllerBuilder class. Controllers are created by the ControllerBuilder class, followed by the DefaultControllerFactory. When you want to create your own instance of a controller, you can use the ExecuteMethod of the controller with your own ControllerContext. You don't want to do that is a real application. When you get some more experience with the framework you will find the appropriate method to do want you want. When you need ControllerContext in Unit test, you can use a mocking framework to mock the ControllerContext or you can class faking it.

ControllerContext 中的 HttpContext 为 null,因为它在创建控制器时未设置。控制器的构造函数没有分配这个属性,所以它将为空。通常,HttpContext 设置为 ControllerBuilder 类的 HttpContext。控制器由 ControllerBuilder 类创建,然后是 DefaultControllerFactory。当您想创建自己的控制器实例时,可以将控制器的 ExecuteMethod 与您自己的 ControllerContext 一起使用。你不想这样做是一个真正的应用程序。当您对框架有更多的经验时,您会找到合适的方法来满足您的需求。当你在单元测试中需要 ControllerContext 时,你可以使用模拟框架来模拟 ControllerContext 或者你可以类伪造它。

You can find a model of the request flow in asp.net mvc on this blog.

您可以在此博客的asp.net mvc 中找到请求流模型。

When your new to Asp.net mvc, it's worth the effort to download the source code and read an trace the route how a request is processed.

当您刚接触 Asp.net mvc 时,下载源代码并阅读跟踪请求处理方式的路径是值得的。

回答by Ben Scheirman

Are you using a controller factory? If so, how are you registering components?

您使用的是控制器工厂吗?如果是这样,您如何注册组件?

I ran into this problem where I had inadvertently added an HttpContext-based dependency as a Singleton, rather than Transient in Windsor.

我遇到了这个问题,我无意中将基于 HttpContext 的依赖项添加为单例,而不是温莎中的瞬态。

HttpContext was null for all but the first request. It took me a while to track down that one.

除了第一个请求之外,所有的 HttpContext 都是空的。我花了一段时间才找到那个。

回答by mmacaulay

Is it that you want to use some functionality from the controller? Or have the controller perform an action?

您是否想使用控制器的某些功能?或者让控制器执行一个动作?

If it's the former, maybe that's some code that should be split out into another class. If it's the latter, you can do this to simply have that controller do a specific action:

如果是前者,也许那是一些应该拆分到另一个类中的代码。如果是后者,您可以这样做以简单地让该控制器执行特定操作:


return RedirectToAction("SomeAction", "SomeOtherController", new {param1 = "Something" });