HttpContext.Current并非无处不在

Posted taobao1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpContext.Current并非无处不在相关的知识,希望对你有一定的参考价值。

无处不在的HttpContext

由于ASP.NET提供了静态属性HttpContext.Current,因此获取HttpContext对象就非常方便了。
也正是因为这个原因,所以我们经常能见到直接访问HttpContext.Current的代码:

public class Class1
{
    public Class1()
    {
        string file = HttpContext.Current.Request.MapPath("~/App_Data/xxxxxx.xml");

        string text = System.IO.File.ReadAllText(file);

        //..........其它的操作
    }

    // 或者在一些方法中直接使用HttpContext.Current
    public void XXXXX()
    {
        string url = HttpContext.Current.Request.RawUrl;

        string username = HttpContext.Current.Session["username"].ToString();

        string value = (string)HttpContext.Current.Items["key"];
    }

    // 甚至还设计成静态属性
    public static string XXX
    {
        get
        {
            return (string)HttpContext.Current.Items["XXX"];
        }
    }
}

这样的代码,经常能在类库项目中看到,由此可见其泛滥程度。

难道这些代码真的没有问题吗?
有人估计会说:我写的代码是给ASP.NET程序使用的,又不是给控制台程序使用,所以没有问题。

真的是这样吗?

 

http://tieba.baidu.com/p/5632998173
http://tieba.baidu.com/p/5634106628
http://www.cnblogs.com/taobao1/p/8722203.html
http://www.cnblogs.com/taobao1/p/8722145.html
https://www.cnblogs.com/baidu1/p/8722089.html
https://www.cnblogs.com/baidu1/p/8722086.html
https://www.cnblogs.com/baidu1/p/8722083.html
https://www.cnblogs.com/baidu1/p/8722078.html
https://www.cnblogs.com/baidu1/p/8722077.html
https://www.cnblogs.com/baidu1/p/8722073.html
https://www.cnblogs.com/baidu1/p/8722071.html
https://www.cnblogs.com/baidu1/p/8722070.html
https://www.cnblogs.com/baidu1/p/8722064.html
https://www.cnblogs.com/baidu1/p/8722059.html
https://www.cnblogs.com/baidu1/p/8722056.html
https://www.cnblogs.com/baidu1/p/8722046.html
https://www.cnblogs.com/baidu1/p/8722039.html
https://www.cnblogs.com/baidu1/p/8722035.html
https://www.cnblogs.com/baidu1/p/8722026.html
https://www.cnblogs.com/baidu1/p/8722022.html
https://www.cnblogs.com/baidu1/p/8722019.html
https://www.cnblogs.com/baidu1/p/8722017.html
https://www.cnblogs.com/baidu1/p/8722015.html
https://www.cnblogs.com/baidu1/p/8722009.html
https://www.cnblogs.com/baidu1/p/8722007.html
https://www.cnblogs.com/baidu1/p/8722001.html
https://www.cnblogs.com/baidu1/p/8721997.html
https://www.cnblogs.com/baidu1/p/8721988.html
https://www.cnblogs.com/baidu1/p/8721984.html
https://www.cnblogs.com/baidu1/p/8721976.html
https://www.cnblogs.com/baidu1/p/8721973.html
https://www.cnblogs.com/baidu1/p/8721967.html
https://www.cnblogs.com/baidu1/p/8721962.html
https://www.cnblogs.com/baidu1/p/8721959.html
https://www.cnblogs.com/baidu1/p/8721950.html
https://www.cnblogs.com/baidu1/p/8721946.html
https://www.cnblogs.com/baidu1/p/8721941.html
https://www.cnblogs.com/baidu1/p/8721939.html
https://www.cnblogs.com/baidu1/p/8721934.html
https://www.cnblogs.com/baidu1/p/8721929.html
https://www.cnblogs.com/baidu1/p/8721919.html
https://www.cnblogs.com/baidu1/p/8721915.html
https://www.cnblogs.com/baidu1/p/8721911.html
https://www.cnblogs.com/baidu1/p/8721909.html
https://www.cnblogs.com/baidu1/p/8721905.html
https://www.cnblogs.com/baidu1/p/8721901.html
https://www.cnblogs.com/baidu1/p/8721894.html
https://www.cnblogs.com/baidu1/p/8721886.html
https://www.cnblogs.com/baidu1/p/8721885.html
https://www.cnblogs.com/baidu1/p/8721875.html
https://www.cnblogs.com/baidu1/p/8721873.html
https://www.cnblogs.com/baidu1/p/8721861.html
https://www.cnblogs.com/baidu1/p/8721856.html
https://www.cnblogs.com/baidu1/p/8721847.html
https://www.cnblogs.com/baidu1/p/8721843.html
https://www.cnblogs.com/baidu1/p/8721833.html
https://www.cnblogs.com/baidu1/p/8721825.html
https://www.cnblogs.com/baidu1/p/8721817.html
https://www.cnblogs.com/baidu1/p/8721809.html
https://www.cnblogs.com/baidu1/p/8721803.html
https://www.cnblogs.com/baidu1/p/8721801.html
https://www.cnblogs.com/baidu1/p/8721795.html
https://www.cnblogs.com/baidu1/p/8721792.html
https://www.cnblogs.com/baidu1/p/8721785.html
https://www.cnblogs.com/baidu1/p/8721782.html
https://www.cnblogs.com/baidu1/p/8721771.html
https://www.cnblogs.com/baidu1/p/8721766.html
https://www.cnblogs.com/baidu1/p/8721759.html
https://www.cnblogs.com/baidu1/p/8721755.html
https://www.cnblogs.com/baidu1/p/8721743.html
https://www.cnblogs.com/baidu1/p/8721737.html
https://www.cnblogs.com/baidu1/p/8721731.html
https://www.cnblogs.com/baidu1/p/8721728.html
https://www.cnblogs.com/baidu1/p/8721719.html
https://www.cnblogs.com/baidu1/p/8721713.html
https://www.cnblogs.com/baidu1/p/8721705.html
https://www.cnblogs.com/baidu1/p/8721700.html
https://www.cnblogs.com/baidu1/p/8721696.html
https://www.cnblogs.com/baidu1/p/8721695.html
https://www.cnblogs.com/baidu1/p/8721688.html
https://www.cnblogs.com/baidu1/p/8721686.html
https://www.cnblogs.com/baidu1/p/8721683.html
https://www.cnblogs.com/baidu1/p/8721679.html
https://www.cnblogs.com/baidu1/p/8721674.html
https://www.cnblogs.com/baidu1/p/8721667.html
https://www.cnblogs.com/baidu1/p/8721657.html
https://www.cnblogs.com/baidu1/p/8721653.html
https://www.cnblogs.com/baidu1/p/8721649.html
https://www.cnblogs.com/baidu1/p/8721644.html
https://www.cnblogs.com/baidu1/p/8721639.html
https://www.cnblogs.com/baidu1/p/8721628.html
https://www.cnblogs.com/baidu1/p/8721624.html
https://www.cnblogs.com/baidu1/p/8721622.html
https://www.cnblogs.com/baidu1/p/8721619.html
https://www.cnblogs.com/baidu1/p/8721611.html
https://www.cnblogs.com/baidu1/p/8721604.html
https://www.cnblogs.com/baidu1/p/8721601.html
https://www.cnblogs.com/baidu1/p/8721594.html
https://www.cnblogs.com/baidu1/p/8721592.html
https://www.cnblogs.com/baidu1/p/8721584.html
https://www.cnblogs.com/baidu1/p/8721580.html
https://www.cnblogs.com/baidu1/p/8721578.html
https://www.cnblogs.com/baidu1/p/8721574.html
https://www.cnblogs.com/baidu1/p/8721567.html
https://www.cnblogs.com/baidu1/p/8721564.html
http://www.cnblogs.com/baidu1/p/8722089.html
http://www.cnblogs.com/baidu1/p/8722086.html
http://www.cnblogs.com/baidu1/p/8722083.html
http://www.cnblogs.com/baidu1/p/8722078.html
http://www.cnblogs.com/baidu1/p/8722077.html
http://www.cnblogs.com/baidu1/p/8722073.html
http://www.cnblogs.com/baidu1/p/8722071.html
http://www.cnblogs.com/baidu1/p/8722070.html
http://www.cnblogs.com/baidu1/p/8722064.html
http://www.cnblogs.com/baidu1/p/8722059.html
http://www.cnblogs.com/baidu1/p/8722056.html
http://www.cnblogs.com/baidu1/p/8722046.html
http://www.cnblogs.com/baidu1/p/8722039.html
http://www.cnblogs.com/baidu1/p/8722035.html
http://www.cnblogs.com/baidu1/p/8722026.html
http://www.cnblogs.com/baidu1/p/8722022.html
http://www.cnblogs.com/baidu1/p/8722019.html
http://www.cnblogs.com/baidu1/p/8722017.html
http://www.cnblogs.com/baidu1/p/8722015.html
http://www.cnblogs.com/baidu1/p/8722009.html
http://www.cnblogs.com/baidu1/p/8722007.html
http://www.cnblogs.com/baidu1/p/8722001.html
http://www.cnblogs.com/baidu1/p/8721997.html
http://www.cnblogs.com/baidu1/p/8721988.html
http://www.cnblogs.com/baidu1/p/8721984.html
http://www.cnblogs.com/baidu1/p/8721976.html
http://www.cnblogs.com/baidu1/p/8721973.html
http://www.cnblogs.com/baidu1/p/8721967.html
http://www.cnblogs.com/baidu1/p/8721962.html
http://www.cnblogs.com/baidu1/p/8721959.html
http://www.cnblogs.com/baidu1/p/8721950.html
http://www.cnblogs.com/baidu1/p/8721946.html
http://www.cnblogs.com/baidu1/p/8721941.html
http://www.cnblogs.com/baidu1/p/8721939.html
http://www.cnblogs.com/baidu1/p/8721934.html
http://www.cnblogs.com/baidu1/p/8721929.html
http://www.cnblogs.com/baidu1/p/8721919.html
http://www.cnblogs.com/baidu1/p/8721915.html
http://www.cnblogs.com/baidu1/p/8721911.html
http://www.cnblogs.com/baidu1/p/8721909.html
http://www.cnblogs.com/baidu1/p/8721905.html
http://www.cnblogs.com/baidu1/p/8721901.html
http://www.cnblogs.com/baidu1/p/8721894.html
http://www.cnblogs.com/baidu1/p/8721886.html
http://www.cnblogs.com/baidu1/p/8721885.html
http://www.cnblogs.com/baidu1/p/8721875.html
http://www.cnblogs.com/baidu1/p/8721873.html
http://www.cnblogs.com/baidu1/p/8721861.html
http://www.cnblogs.com/baidu1/p/8721856.html
http://www.cnblogs.com/baidu1/p/8721847.html
http://www.cnblogs.com/baidu1/p/8721843.html
http://www.cnblogs.com/baidu1/p/8721833.html
http://www.cnblogs.com/baidu1/p/8721825.html
http://www.cnblogs.com/baidu1/p/8721817.html
http://www.cnblogs.com/baidu1/p/8721809.html
http://www.cnblogs.com/baidu1/p/8721803.html
http://www.cnblogs.com/baidu1/p/8721801.html
http://www.cnblogs.com/baidu1/p/8721795.html
http://www.cnblogs.com/baidu1/p/8721792.html
http://www.cnblogs.com/baidu1/p/8721785.html
http://www.cnblogs.com/baidu1/p/8721782.html
http://www.cnblogs.com/baidu1/p/8721771.html
http://www.cnblogs.com/baidu1/p/8721766.html
http://www.cnblogs.com/baidu1/p/8721759.html
http://www.cnblogs.com/baidu1/p/8721755.html
http://www.cnblogs.com/baidu1/p/8721743.html
http://www.cnblogs.com/baidu1/p/8721737.html
http://www.cnblogs.com/baidu1/p/8721731.html
http://www.cnblogs.com/baidu1/p/8721728.html
http://www.cnblogs.com/baidu1/p/8721719.html
http://www.cnblogs.com/baidu1/p/8721713.html
http://www.cnblogs.com/baidu1/p/8721705.html
http://www.cnblogs.com/baidu1/p/8721700.html
http://www.cnblogs.com/baidu1/p/8721696.html
http://www.cnblogs.com/baidu1/p/8721695.html
http://www.cnblogs.com/baidu1/p/8721688.html
http://www.cnblogs.com/baidu1/p/8721686.html
http://www.cnblogs.com/baidu1/p/8721683.html
http://www.cnblogs.com/baidu1/p/8721679.html
http://www.cnblogs.com/baidu1/p/8721674.html
http://www.cnblogs.com/baidu1/p/8721667.html
http://www.cnblogs.com/baidu1/p/8721657.html
http://www.cnblogs.com/baidu1/p/8721653.html
http://www.cnblogs.com/baidu1/p/8721649.html
http://www.cnblogs.com/baidu1/p/8721644.html
http://www.cnblogs.com/baidu1/p/8721639.html
http://www.cnblogs.com/baidu1/p/8721628.html
http://www.cnblogs.com/baidu1/p/8721624.html
http://www.cnblogs.com/baidu1/p/8721622.html
http://www.cnblogs.com/baidu1/p/8721619.html
http://www.cnblogs.com/baidu1/p/8721611.html
http://www.cnblogs.com/baidu1/p/8721604.html
http://www.cnblogs.com/baidu1/p/8721601.html
http://www.cnblogs.com/baidu1/p/8721594.html
http://www.cnblogs.com/baidu1/p/8721592.html
http://www.cnblogs.com/baidu1/p/8721584.html
http://www.cnblogs.com/baidu1/p/8721580.html
http://www.cnblogs.com/baidu1/p/8721578.html
http://www.cnblogs.com/baidu1/p/8721574.html
http://www.cnblogs.com/baidu1/p/8721567.html
http://www.cnblogs.com/baidu1/p/8721564.html
www.cnblogs.com/baidu1/p/8722089.html
www.cnblogs.com/baidu1/p/8722086.html
www.cnblogs.com/baidu1/p/8722083.html
www.cnblogs.com/baidu1/p/8722078.html
www.cnblogs.com/baidu1/p/8722077.html
www.cnblogs.com/baidu1/p/8722073.html
www.cnblogs.com/baidu1/p/8722071.html
www.cnblogs.com/baidu1/p/8722070.html
www.cnblogs.com/baidu1/p/8722064.html
www.cnblogs.com/baidu1/p/8722059.html
www.cnblogs.com/baidu1/p/8722056.html
www.cnblogs.com/baidu1/p/8722046.html
www.cnblogs.com/baidu1/p/8722039.html
www.cnblogs.com/baidu1/p/8722035.html
www.cnblogs.com/baidu1/p/8722026.html
www.cnblogs.com/baidu1/p/8722022.html
www.cnblogs.com/baidu1/p/8722019.html
www.cnblogs.com/baidu1/p/8722017.html
www.cnblogs.com/baidu1/p/8722015.html
www.cnblogs.com/baidu1/p/8722009.html
www.cnblogs.com/baidu1/p/8722007.html
www.cnblogs.com/baidu1/p/8722001.html
www.cnblogs.com/baidu1/p/8721997.html
www.cnblogs.com/baidu1/p/8721988.html
www.cnblogs.com/baidu1/p/8721984.html
www.cnblogs.com/baidu1/p/8721976.html
www.cnblogs.com/baidu1/p/8721973.html
www.cnblogs.com/baidu1/p/8721967.html
www.cnblogs.com/baidu1/p/8721962.html
www.cnblogs.com/baidu1/p/8721959.html
www.cnblogs.com/baidu1/p/8721950.html
www.cnblogs.com/baidu1/p/8721946.html
www.cnblogs.com/baidu1/p/8721941.html
www.cnblogs.com/baidu1/p/8721939.html
www.cnblogs.com/baidu1/p/8721934.html
www.cnblogs.com/baidu1/p/8721929.html
www.cnblogs.com/baidu1/p/8721919.html
www.cnblogs.com/baidu1/p/8721915.html
www.cnblogs.com/baidu1/p/8721911.html
www.cnblogs.com/baidu1/p/8721909.html
www.cnblogs.com/baidu1/p/8721905.html
www.cnblogs.com/baidu1/p/8721901.html
www.cnblogs.com/baidu1/p/8721894.html
www.cnblogs.com/baidu1/p/8721886.html
www.cnblogs.com/baidu1/p/8721885.html
www.cnblogs.com/baidu1/p/8721875.html
www.cnblogs.com/baidu1/p/8721873.html
www.cnblogs.com/baidu1/p/8721861.html
www.cnblogs.com/baidu1/p/8721856.html
www.cnblogs.com/baidu1/p/8721847.html
www.cnblogs.com/baidu1/p/8721843.html
www.cnblogs.com/baidu1/p/8721833.html
www.cnblogs.com/baidu1/p/8721825.html
www.cnblogs.com/baidu1/p/8721817.html
www.cnblogs.com/baidu1/p/8721809.html
www.cnblogs.com/baidu1/p/8721803.html
www.cnblogs.com/baidu1/p/8721801.html
www.cnblogs.com/baidu1/p/8721795.html
www.cnblogs.com/baidu1/p/8721792.html
www.cnblogs.com/baidu1/p/8721785.html
www.cnblogs.com/baidu1/p/8721782.html
www.cnblogs.com/baidu1/p/8721771.html
www.cnblogs.com/baidu1/p/8721766.html
www.cnblogs.com/baidu1/p/8721759.html
www.cnblogs.com/baidu1/p/8721755.html
www.cnblogs.com/baidu1/p/8721743.html
www.cnblogs.com/baidu1/p/8721737.html
www.cnblogs.com/baidu1/p/8721731.html
www.cnblogs.com/baidu1/p/8721728.html
www.cnblogs.com/baidu1/p/8721719.html
www.cnblogs.com/baidu1/p/8721713.html
www.cnblogs.com/baidu1/p/8721705.html
www.cnblogs.com/baidu1/p/8721700.html
www.cnblogs.com/baidu1/p/8721696.html
www.cnblogs.com/baidu1/p/8721695.html
www.cnblogs.com/baidu1/p/8721688.html
www.cnblogs.com/baidu1/p/8721686.html
www.cnblogs.com/baidu1/p/8721683.html
www.cnblogs.com/baidu1/p/8721679.html
www.cnblogs.com/baidu1/p/8721674.html
www.cnblogs.com/baidu1/p/8721667.html
www.cnblogs.com/baidu1/p/8721657.html
www.cnblogs.com/baidu1/p/8721653.html
www.cnblogs.com/baidu1/p/8721649.html
www.cnblogs.com/baidu1/p/8721644.html
www.cnblogs.com/baidu1/p/8721639.html
www.cnblogs.com/baidu1/p/8721628.html
www.cnblogs.com/baidu1/p/8721624.html
www.cnblogs.com/baidu1/p/8721622.html
www.cnblogs.com/baidu1/p/8721619.html
www.cnblogs.com/baidu1/p/8721611.html
www.cnblogs.com/baidu1/p/8721604.html
www.cnblogs.com/baidu1/p/8721601.html
www.cnblogs.com/baidu1/p/8721594.html
www.cnblogs.com/baidu1/p/8721592.html
www.cnblogs.com/baidu1/p/8721584.html
www.cnblogs.com/baidu1/p/8721580.html
www.cnblogs.com/baidu1/p/8721578.html
www.cnblogs.com/baidu1/p/8721574.html
www.cnblogs.com/baidu1/p/8721567.html
www.cnblogs.com/baidu1/p/8721564.html

以上是关于HttpContext.Current并非无处不在的主要内容,如果未能解决你的问题,请参考以下文章

asp.net mvc中在使用async的时候HttpContext为null的问题

路由请求时 HttpContext.Current.Session 为空

HttpSelfHostServer 和 HttpContext.Current

HttpContext.Current 在异步回调中为空

使用 IAuthenticationFilter 时 HttpContext.Current 为空

HttpContext.Current.Server未将对象引用到实例