这可能不是使用控制器的正确方法,但我确实注意到了这个问题并且没有想出纠正它的方法。
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
}
我注意到控制器上的 HttpContext 不是您在 System.Web.HttpContext.Current 中找到的“实际”HttpContext。
有没有办法在控制器上手动填充 HttpContextBase?还是创建控制器实例的更好方法?