如何在ASP.NET Core中获取HttpContext.Current? [英] How to get HttpContext.Current in ASP.NET Core?

查看:808
本文介绍了如何在ASP.NET Core中获取HttpContext.Current?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前正在使用ASP.NET Core重写/转换我们的ASP.NET WebForms应用程序。



在一节中,我们在类中使用 HttpContext 库以检查当前状态。如何在.NET Core 1.0中访问 HttpContext.Current

  var current = HttpContext.Current; 
if(current == null)
{
//在这里做些事情
//字符串连接= Configuration.GetConnectionString( MyDb);
}

为了构建当前的应用程序主机,我需要访问它。

  $ {current.Request.Url.Scheme}:// {{current.Request.Url.Host} {{current.Request。 Url.Port == 80?:: + current.Request.Url.Port)}; 


解决方案

作为一般规则,转换Web窗体或MVC5

HttpContext.Current 已在ASP.NET Core中删除。从单独的类库访问当前HTTP上下文是ASP.NET Core试图避免的混乱体系结构类型。有几种方法可以在ASP.NET Core中对其进行重新架构。



HttpContext属性



您可以通过任何控制器上的 HttpContext 属性访问当前的HTTP上下文。与原始代码示例最接近的事情是将 HttpContext 传递给您所调用的方法:

 公共类HomeController:控制器
{
public IActionResult Index()
{
MyMethod(HttpContext);

//其他代码
}
}

public void MyMethod(Microsoft.AspNetCore.Http.HttpContext上下文)
{
var host = $ {context.Request.Scheme}:// {{context.Request.Host};

//其他代码
}



中间件中的HttpContext参数



如果您正在编写用于ASP.NET Core管道的自定义中间件,当前请求的 HttpContext 被传递到您的自动调用方法:

 公共任务调用(HttpContext上下文)
{
//使用当前HTTP上下文执行操作...
}



HTTP上下文访问器



最后,您可以使用 IHttpContextAccessor 帮助程序服务在任何托管类中获取HTTP上下文由ASP.NET Core依赖项注入系统提供。当您拥有控制器使用的通用服务时,此功能很有用。



在构造函数中请求此接口:

  public MyMiddleware(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}

然后您可以通过一种安全的方式访问当前的HTTP上下文:

  var context = _httpContextAccessor.HttpContext; 
//使用当前HTTP上下文执行操作...

IHttpContextAccessor 并非总是默认添加到服务容器中,因此为了安全起见,请在 ConfigureServices 中进行注册:

  public void ConfigureServices(IServiceCollection服务)
{
services.AddHttpContextAccessor();
//如果< .NET Core 2.2使用此
//services.TryAddSingleton<IHttpContextAccessor,HttpContextAccessor>();。

//其他代码...
}


We are currently rewriting/converting our ASP.NET WebForms application using ASP.NET Core. Trying to avoid re-engineering as much as possible.

There is a section where we use HttpContext in a class library to check the current state. How can I access HttpContext.Current in .NET Core 1.0?

 var current = HttpContext.Current;
     if (current == null)
      {
       // do something here
       // string connection = Configuration.GetConnectionString("MyDb");
      }

I need to access this in order to construct current application host.

$"{current.Request.Url.Scheme}://{current.Request.Url.Host}{(current.Request.Url.Port == 80 ? "" : ":" + current.Request.Url.Port)}";

解决方案

As a general rule, converting a Web Forms or MVC5 application to ASP.NET Core will require a significant amount of refactoring.

HttpContext.Current was removed in ASP.NET Core. Accessing the current HTTP context from a separate class library is the type of messy architecture that ASP.NET Core tries to avoid. There are a few ways to re-architect this in ASP.NET Core.

HttpContext property

You can access the current HTTP context via the HttpContext property on any controller. The closest thing to your original code sample would be to pass HttpContext into the method you are calling:

public class HomeController : Controller
{
    public IActionResult Index()
    {
        MyMethod(HttpContext);

        // Other code
    }
}

public void MyMethod(Microsoft.AspNetCore.Http.HttpContext context)
{
    var host = $"{context.Request.Scheme}://{context.Request.Host}";

    // Other code
}

HttpContext parameter in middleware

If you're writing custom middleware for the ASP.NET Core pipeline, the current request's HttpContext is passed into your Invoke method automatically:

public Task Invoke(HttpContext context)
{
    // Do something with the current HTTP context...
}

HTTP context accessor

Finally, you can use the IHttpContextAccessor helper service to get the HTTP context in any class that is managed by the ASP.NET Core dependency injection system. This is useful when you have a common service that is used by your controllers.

Request this interface in your constructor:

public MyMiddleware(IHttpContextAccessor httpContextAccessor)
{
    _httpContextAccessor = httpContextAccessor;
}

You can then access the current HTTP context in a safe way:

var context = _httpContextAccessor.HttpContext;
// Do something with the current HTTP context...

IHttpContextAccessor isn't always added to the service container by default, so register it in ConfigureServices just to be safe:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpContextAccessor();
    // if < .NET Core 2.2 use this
    //services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

    // Other code...
}

这篇关于如何在ASP.NET Core中获取HttpContext.Current?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆