本文转自:http://www.spaprogrammer.com/2015/07/mvc-6-httpcontextcurrent.html

As you know with MVC 6, HttpContext is rewritten and one of the most used functionality , HttpContext.Current is dropped. In most situations you don’t need this and you should use Controller context. But in some situations you may need access to HttpContext. In my scenario I want to pass some data using HttpContext.Items from one layer (middleware) to another (business layer).
 
Below are the two ways to get current HttpContext:
 
 
  • Using dependency injection we can inject the HttpContext on to the service directly. DI injects a singleton object called HttpContextAccessor which is used to retrieve the current HttpContext. Here is the sample code for this
    public class MyService
    {
        private HttpContext context;
        public MySerivce(IHttpContextAccessor contextAccessor)
        {
            this.context = contextAccessor.HttpContext;
        }
 
 
  • Similar to HttpContext.Current we can also create a static class that holds the reference for the current HttpContext. Here is the code for this static class
    public static class HttpHelper
    {
        private static IHttpContextAccessor HttpContextAccessor;
        public static void Configure(IHttpContextAccessor httpContextAccessor)
        {
            HttpContextAccessor = httpContextAccessor;
        }
 
        public static HttpContext HttpContext
        {
            get
            {
                return HttpContextAccessor.HttpContext;
            }
        }
    }
 
This static HttpHelper class stores the singleton HttpContextAccessor and is then used to return the current HttpContext. We will configure our HttpHelper in our startup as shown below
 
        public void Configure(IApplicationBuilder app)
        {
            //other code             
    HttpHelper.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>());
        }
 
Finally we can get current HttpContext wherever we need using our Helper as below
HttpHelper.HttpContext
 
As you see using the above two approaches to get the current HttpContext.
Please see my updated post on this. I am using CallContext to pass the HttpContext and other data ASP.NET Core Async Context Data

[转]ASP.NET Core / MVC 6 HttpContext.Current的更多相关文章

  1. ASP.NET Core开发之HttpContext

    ASP.NET Core中的HttpContext开发,在ASP.NET开发中我们总是会经常用到HttpContext. 那么在ASP.NET Core中要如何使用HttpContext呢,下面就来具 ...

  2. ASP.NET Core MVC 授权的扩展:自定义 Authorize Attribute 和 IApplicationModelProvide

    一.概述 ASP.NET Core MVC 提供了基于角色( Role ).声明( Chaim ) 和策略 ( Policy ) 等的授权方式.在实际应用中,可能采用部门( Department , ...

  3. 【转】ASP.NET Core开发之HttpContext

    ASP.NET Core中的HttpContext开发,在ASP.NET开发中我们总是会经常用到HttpContext. 那么在ASP.NET Core中要如何使用HttpContext呢,下面就来具 ...

  4. ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)

    ASP.NET MVC 一.获取URL(ASP.NET通用): [1]获取完整url(协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [ ...

  5. Kubernetes中分布式存储Rook-Ceph的使用:一个ASP.NET Core MVC的案例

    在<Kubernetes中分布式存储Rook-Ceph部署快速演练>文章中,我快速介绍了Kubernetes中分布式存储Rook-Ceph的部署过程,这里介绍如何在部署于Kubernete ...

  6. 解决ASP.NET Core Mvc文件上传限制问题

    一.简介 在ASP.NET Core MVC中,文件上传的最大上传文件默认为20MB,如果我们想上传一些比较大的文件,就不知道怎么去设置了,没有了Web.Config我们应该如何下手呢? 二.设置上传 ...

  7. asp.net core mvc权限控制:在视图中控制操作权限

    在asp.net core mvc中提供了权限验证框架,前面的文章中已经介绍了如何进行权限控制配置,权限配置好后,权限验证逻辑自动就会执行,但是在某些情况下,我们可能需要在代码里或者视图中通过手工方式 ...

  8. asp.net core mvc剖析:启动流程

    asp.net core mvc是微软开源的跨平台的mvc框架,首先它跟原有的MVC相比,最大的不同就是跨平台,然后又增加了一些非常实用的新功能,比如taghelper,viewcomponent,D ...

  9. asp.net core mvc实现伪静态功能

    在大型网站系统中,为了提高系统访问性能,往往会把一些不经常变得内容发布成静态页,比如商城的产品详情页,新闻详情页,这些信息一旦发布后,变化的频率不会很高,如果还采用动态输出的方式进行处理的话,肯定会给 ...

随机推荐

  1. cdq分治略解

    前言 陌上花开,可缓缓归矣                         --吴越王 寓意:意思是:田间阡陌上的花开了,你可以一边赏花,一边慢慢回来. 隐意:春天都到了,你怎么还没有回来.形容吴越王 ...

  2. Mac php7本地安装mongodb扩展以适配使用mongo扩展的线上老代码

    从https://pecl.php.net/package/mongodb官网下载压缩包(不懂事的我下载了1.5.1版本) 解压安装包 tar -zxvf mongodb-1.5.1.tgz 进入解压 ...

  3. 移动端复制粘贴clipboardjs

    官方网址: https://clipboardjs.com/ 不能一打开就直接触发复制粘贴: 参考博客: http://blog.csdn.net/twoByte/article/details/52 ...

  4. Dubbo基础配置

    服务启动检查配置 默认check=true dubbo:reference  check=“false” 关闭某个服务的启动时检查:(没有提供者时报错) dubbo:consumer  check=“ ...

  5. vue从一个页面跳转到另一个页面并携带参数

    1.需求: 点击商场跳转到商业体列表 解决方案: 元页面: a标签中添加跳转函数 <a class="orderBtn1 sIRicon2" href="javas ...

  6. Kibana6.x.x源码分析--启动时无反应分析

    今天执行启动命令后,不报错,但是也没有反应,一时不知道是什么原因造成的,后来经过分析发现,无意间删除了根目录下的一个文件夹plugins,重新创建上这个文件夹后,再次运行就OK了.

  7. opacity 兼容 ie8

    opacity: 0.6; filter: alpha(opacity=60);

  8. sql语句中变量的写法

        $sql = "update cat set num=num+1 where cat_id=$art[cat_id]";    $sql = "update ca ...

  9. Java 安全套接字编程以及 keytool 使用最佳实践

    Java 安全套接字编程以及 keytool 使用最佳实践 http://www.ibm.com/developerworks/cn/java/j-lo-socketkeytool/

  10. Oracle中对XMLType的简单操作(extract、extractvalue)

    前几天一直在做Oracle对XMLType字段的操作,我还不是Oracle大拿,到网上找了很多资料,但是很多就是单一功能的介绍,不能很好的解决问题,现在在这里总结下. 1.下面先创建一个名未test. ...