Search code examples
c#asp.net-corexunit

What is the concept of 'DefaultHttpContext' in ASP.NET Core?


I am searching for understanding the concept of DefaultHttpContext. What is this in simple terms?

There is a sample code below:

           var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] {
                                        new Claim(ClaimTypes.NameIdentifier, "E59899E9-AAFD-4AA6-B32E-12A27A4FA43D"),
                                        new Claim(ClaimTypes.Name, "[email protected]")
                                        // other required and custom claims
                                   }, "TestAuthentication"));

            var httpContext = new DefaultHttpContext()
            {
                User = user
            };


            /// passed a ControllerContext which defines the controller to be created
            controllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext()
            {
                HttpContext = httpContext,
            };

Solution

  • What is DefaultHttpContext

    The DefaultHttpContext class is the default implementation of the abstract HttpContext class that is included in ASP.NET Core.

    What is the concept of 'DefaultHttpContext' in ASP.NET Core?

    Actually, HttpContext Initializes a new instance of the DefaultHttpContext class. "When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response".

    Note: You can have a look more details at the official document here.