System.Web.HttpContext.Current不存在

one*_*com 7 c# asp.net

我正在尝试使用visual studio 2015在asp.net mvc应用程序中获取有关我的用户的一些信息.

当我试图从请求中获取信息时

    System.Web.HttpContext.Current req = new System.Web.HttpContext.Current();
Run Code Online (Sandbox Code Playgroud)

我收到一个错误 Error CS0426 The type name 'Current' does not exist in the type 'HttpContext'

有人知道怎么修这个东西吗?

Arg*_*a C 9

如果你想获得当前的上下文

System.Web.HttpContext currentContext = System.Web.HttpContext.Current;
Run Code Online (Sandbox Code Playgroud)

如果你想创建一个(出于某种原因,如测试)

System.Web.HttpContext newContext = new System.Web.HttpContext(
    new System.Web.HttpRequest("", "http://example.com", ""),
    new System.Web.HttpResponse(new System.IO.StringWriter())
    );
Run Code Online (Sandbox Code Playgroud)

  • 嗯,不,不是答案。这是不可能的,因为无论什么深不可测的原因,Current都不存在于HttpContext中。所以你不能只去`System.Web.HttpContext currentContext = System.Web.HttpContext.Current`,因为current不存在。我不知道为什么它不存在,这就是这里的问题。 (2认同)