HttpContext of ASP. NET Core Development

Source: Internet
Author: User

The development of HttpContext in ASP. HttpContext is always used in ASP.

So how do you use HttpContext in ASP. NET core, the following is the specific learning of ASP. HttpContext.

Inject Httpcontextaccessor

An Ihttpcontextaccessor interface is provided in ASP. Httpcontextaccessor, which simplifies access HttpContext by default.

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

Services. Addsingleton<ihttpcontextaccessor, httpcontextaccessor> ();

Get Httpcontextaccessor

Here's an actual operation to get httpcontextaccessor.

Create a new ASP. NET Core Web application and select the Web application. Authentication is checked for non-authentication.

Then add the following code to the HomeController:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

    public class homecontroller : controller    {         private IHttpContextAccessor _accessor;         public homecontroller (ihttpcontextaccessor accessor)          {            _ accessor = accessor;        }         public iactionresult index ()         {             var httpcontext = _ Accessor. Httpcontext;            return view ();         }    }

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

So we can get to HttpContext.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/443844/201705/443844-20170504091837070-175893397. PNG "style=" margin:0px;padding:0px;border:0px; "/>

The HttpContext object can be obtained after running. It also says that it must be injected at program start time to get to httpcontextaccessor.

So where is the new program injected?

This is the

public void Configureservices (iservicecollection services) {//ADD framework services. Services.        Addmvc (); }

Services. Injected into the ADDMVC ().

Implement HttpContext.Current

When you use HttpContext.Current to get HttpContext in ASP. NET core is now not doing so.

However, if you still want to use static httpcontext.current, reduce the cost of migrating old programs, or can be achieved.

Create a new static HttpContext class,

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

     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;         }    } 

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Then add an extension class.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

    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;         }    }

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

The call can then be made in the startup class.

By default, if you call Usestatichttpcontext () directly in an MVC project.

public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { App. Usestatichttpcontext ();

In projects that do not inject httpcontextaccessor, you also need to call the Configureservices method in the

Services. Addhttpcontextaccessor ();

You can then use the httpcontext.current in other places.

Public Iactionresult Index () {var statichttpcontext = HttpContextDemo.HttpContext.Current;        return View (); }

The demo here is called in the controller, in fact, more is called in other places, such as middleware and some of their own written service.

There is a HttpContext attribute in the Controller that can actually be used directly in the Httpcontext,controllerbase class.


HttpContext of ASP. NET Core Development

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.