Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext in .net standard library

I am working on couple of projects one of which is an ASP.NET 4.5 application and other one is .Net Core API 1.1 project. The asp.net application is using HttpContext classes to read cookies and page headers. Now, I need to move this to a .net standard library which can be used by both the project. I don't find HttpContext in .net standard SDK. Any suggestions?

like image 428
Kumar A Avatar asked Nov 17 '17 19:11

Kumar A


People also ask

What is HttpContext current in C#?

This property is a static property of the HttpContext class. The property stores the HttpContext instance that applies to the current request. The properties of this instance are the non-static properties of the HttpContext class. You can also use the Page.

What is HttpContext application?

HttpContext. Current. Application is simply a reference to the static global HttpApplicationState object in . NET for your Web Application, of which there should be one global instance per web application. By storing data there, you provide fast, thread-safe access to your global variables.

How use HttpContext current in .NET Core?

ASP.NET Core apps access HttpContext through the IHttpContextAccessor interface and its default implementation HttpContextAccessor. It's only necessary to use IHttpContextAccessor when you need access to the HttpContext inside a service.

When should I use HttpContext items?

Similarly, you use HTTPContext Items collection when you are sharing the same information across the different instance based on the user request and that request could be changed for a different request.


2 Answers

There's a problem to your approach: .NET Standard is the most bare-bones implementation of .NET available, meaning that only basic features which are platform- and scenario-agnostic are implemented.
HttpContext exists on both the .NET Framework and .NET Core (both of which implement .NET Standard, by the way), but being specific to the Web, it does not exist on .NET Standard.

So, you have three options:

  1. Target the .NET Framework and use System.Web.HttpContext
  2. Target .NET Core and use Microsoft.AspNetCore.Http.HttpContext
  3. Move the logic that uses HttpContext away from the .NET Standard project

Do notice, though, that those classes vary greatly. The .NET Core version was created for ASP.NET Core which is vastly different to ASP.NET 4.5 and olders.

like image 174
Camilo Terevinto Avatar answered Sep 29 '22 22:09

Camilo Terevinto


I do not agree with these answers and the non-sense about only having HttpContext in the web project. That is actually tightly coupled as YOU want to be able to re-use code and thus a class library in .net core OR .net standard SHOULD be able to use the HttpContext

So in a .NET Standard you want to add in :

Microsoft.AspNetCore.Http.Abstractions 

Sadly though there is so much different about the new HttpContext HttpContext.Current is missing and thus session and request etc..

(Yes, I get the design patterns and why to separate out and make it more testable)

Here is a little trick you can pull off as well to get Current

namespace System.Web {     public static class HttpContext         {             private static Microsoft.AspNetCore.Http.IHttpContextAccessor m_httpContextAccessor;       public static void Configure(Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor)         {             m_httpContextAccessor = httpContextAccessor;         }           public static Microsoft.AspNetCore.Http.HttpContext Current         {             get             {                 return m_httpContextAccessor.HttpContext;             }         }      } } 
like image 23
Tom Stickel Avatar answered Sep 29 '22 22:09

Tom Stickel