HttpContext in .net standard library

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

+10
source share
5 answers

The problem with your approach: .NET Standard is the most affordable version of .NET for .NET, which means that only basic functions that are platform and agnostic script are implemented. HttpContext exists for both the .NET Framework and the .NET Core (both of which implement the .NET standard, by the way), but, being specific to the Internet, it does not exist in the .NET Standard.

So, you have three options:

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

Please note that these classes are very different. The .NET Core version was created for the ASP.NET kernel, which is very different from ASP.NET 4.5 and older.

+13
source

Starting with .NETStandard 2.0 you can work with HttpContext .

Your .csproj file will look something like this:

 <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> </Project> 

Note. This is an empty project file, so you may have other dependencies (i.e. <ItemGroup>...</ItemGroup> )

To install the required nuget package, simply run the following dotnet cli command or install using the NuGet package manager.

 dotnet add package Microsoft.AspNetCore.Http.Abstractions 
+4
source

I need to transfer this [reading cookies and page headers] to the standard .NET library that can be used by both projects ...

Do not do that.

Suppose you perform an operation X on the data that you read from cookies. You can move this operation X to the library. The challenge for ASP.NET projects is to process the request pipeline. Reading cookies belongs to.

+1
source

I agree with the answer above if you want netstandard libraries to be simple and without web requests, if possible. There is a System.Net.Http nuget package that is designed for the standard .net standard. This can be used when upgrading 4.6.x for the full structure and .net kernel if you are creating a library and do not want the overhead of the asp.net kernel.

0
source

I disagree with these answers and the pointlessness of having only an HttpContext in a web project. This is actually closely related, since YOU want to be able to reuse the code and therefore the class library in the .net core OR .net standard MUST be able to use HttpContext

So, in .NET Standard you want to add:

 Microsoft.AspNetCore.Http.Abstractions 

Unfortunately, although there are so many differences in the new HttpContext, HttpContext.Current missing and therefore the session, request, etc.

(Yes, I get design patterns and why highlight them and make them more testable)

Here is a little trick you can do 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; } } } } 
0
source

Source: https://habr.com/ru/post/1273477/


All Articles