ASP.NET Core development of HttpContext

HttpContext development of ASP.NET Core, in ASP.NET development, we always will be frequently used HttpContext.

So how do you use it in HttpContext in ASP.NET Core, here's a specific learning ASP.NET Core HttpContext.

Injection HttpContextAccessor

ASP.NET provides a medium-Core IHttpContextAccessor interfaces, HttpContextAccessor default implemented it simplifies access HttpContext.

It must be registered in IServicesCollection at the time the program starts, so you can get to HttpContextAccessor in the program, and used to access the HttpContext.

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

Get HttpContextAccessor

Let's do an actual operation, get HttpContextAccessor.

Create a new ASP.NET Core Web Application, select the Web application. Authentication is not checked for authentication.

HomeController then add the following code:

public class HomeController : Controller
    {
        private IHttpContextAccessor _accessor;
        public HomeController(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }
        public IActionResult Index()
        {
            var httpcontext = _accessor.HttpContext;
            return View();
        }
    }

So you can get to the HttpContext

public IActionResut Index()
{
   var httpConetext = _accessor.HttpContext;
   return View();  
}

After the run will be able to get to the HttpContext object. Also said above, it must be injected in order to get to HttpContextAccessor when the program starts.

So the new program in which it is injected.

Here is the 

public void ConfigureServices(IServiceCollection services)
{
   // Add framework services.
   services.AddMvc();
}

services.AddMvc () injected.

Achieve HttpContext.Current

We get a lot of use HttpContext.Current HttpContext in ASP.NET, ASP.NET Core now has not done so.

But if you still want to use a static HttpContext.Current, reduce the cost of migrating the old program, or can be achieved.

Create a static HttpContext class,

   public static class HttpContext
    {
        private static IHttpContextAccessor _accessor;

        public static Microsoft.AspNetCore.Http.HttpContext Current => _accessor.HttpContext;

        internal static void Configure(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }
    }

Then subsequently add an extension class.

public static class StaticHttpContextExtensions
    {
        public static void AddHttpContextAccessor(this IServiceCollection services)
        {
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }

        public static IApplicationBuilder UseStaticHttpContext(this IApplicationBuilder app)
        {
            var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
            HttpContext.Configure(httpContextAccessor);
            return app;
        }
    }

Then it can be called in the Startup category.

By default, if you call UseStaticHttpContext directly in the MVC project () can be.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseStaticHttpContext();

In the project without injecting HttpContextAccessor, the method needs to call ConfigureServices

services.AddHttpContextAccessor();

You can then use HttpContext.Current elsewhere.

        public IActionResult Index()
        {
            var statichttpcontext = HttpContextDemo.HttpContext.Current;
            return View();
        }

Demonstrated here it is to call in the Controller, in fact, more is to call in other places, such as middleware and some of his own writing Service.

In fact, the Controller can be used directly HttpContext, ControllerBase HttpContext class has a property.

Guess you like

Origin www.cnblogs.com/sylone/p/11481446.html