HttpContext and corehttpcontext for ASP. NET Core development

Source: Internet
Author: User

HttpContext and corehttpcontext for ASP. NET Core development

HttpContext development in ASP. NET Core is often used in ASP. NET development.

So how to use HttpContext in ASP. NET Core? Next we will learn ASP. NET Core HttpContext in detail.

HttpContextAccessor Injection

ASP. NET Core provides an IHttpContextAccessor interface, which simplifies access to HttpContext by default.

It must be registered in IServicesCollection when the program is started, so that HttpContextAccessor can be obtained in the program and used to access HttpContext.

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

 

Obtain HttpContextAccessor

Perform the following operations to obtain HttpContextAccessor.

Create an ASP. NET Core Web application and select Web application. If this option is selected, no authentication is performed.

Then add the following code to HomeController:

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

In this way, the HttpContext can be obtained.

The HttpContext object can be obtained after running. As mentioned above, HttpContextAccessor can be obtained only when the program is injected at startup.

Where is the newly created program injected.

Here is

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

Services. AddMvc.

 

Implement HttpContext. Current

HttpContext. Current is widely used in ASP. NET to obtain HttpContext. Now ASP. NET Core does not.

However, if you still want to use static HttpContext. Current to reduce the cost of migrating the old program, you can still achieve it.

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 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 you can call it in the Startup class.

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

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

In a project without HttpContextAccessor injection, you must also call the ConfigureServices method.

services.AddHttpContextAccessor();

Then you can use HttpContext. Current elsewhere.

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

The demo here is calling in the Controller. In fact, it is more called in other places, such as middleware and some self-written services.

HttpContext can be directly used in Controller, and the ControllerBase class has an HttpContext attribute.

 

If you think this article is helpful to you, click"Recommendation", Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.