加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 18:55:47 所属栏目:asp.Net 来源:网络整理
导读:这可能不是正确的方法使用控制器,但我没有注意到这个问题,并没有想出一种方法来纠正它。 public JsonResult SomeControllerAction() { //The current method has the HttpContext just fine bool currentIsNotNull = (this.HttpContext == null); //which
这可能不是正确的方法使用控制器,但我没有注意到这个问题,并没有想出一种方法来纠正它。
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不是“实际的”HttpContext,你会发现System.Web.HttpContext.Current。

有没有办法手动填充控制器上的HttpContextBase?或者更好的方法来创建一个Controller的实例?

解决方法

控制器的设计不是像你一样手动创建。听起来像你真正应该做的是把任何可重用的逻辑你有一个帮助类,而不是。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读