这可能不是使用控制器的正确方法,但我确实注意到了这个问题,而且还没有找到纠正它的方法。
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?或者有更好的方法来创建控制器的实例吗?
(HttpContextBase)
的转换是多余的,请将其删除。 - Andrus