HttpContext in .net standard library

28,388

Solution 1

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.

Solution 2

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

    }
}

Solution 3

If you're targetting .NETStandard 2.0 you can work with HttpContext.

Though it is not included in the .NET Standard library, the Microsoft.AspNetCore.Http.Abstractions package (which targets .NET Standard 2.0) will enable you to work with HttpContext.

Solution 4

I need to move this [read cookies and page headers] to a .NET standard library which can be used by both the project...

Do NOT do this.

Let's assume you're performing an operation X on the data you read from the cookies. You may move this operation X to the library instead. The job of the ASP.NET projects is to handle the request pipeline. Reading the cookies belongs there.

Share:
28,388
Kumar A
Author by

Kumar A

Updated on September 02, 2021

Comments

  • Kumar A
    Kumar A over 2 years

    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?

  • House of Dexter
    House of Dexter over 5 years
    because this is not the same HttpContext. (I did not downvote you)
  • Justin
    Justin over 5 years
    This HttpContext only has Equals and ReferenceEquals methods available, no properties for accessing the runtime. What am I missing?
  • AleX_
    AleX_ over 4 years
    I get null reference exception on calling the Current property. Where and how is the best practice to init this?
  • Atul
    Atul over 4 years
    same here, while this looks like it should work, I am getting null reference exception on calling the Current property as well
  • Manfred
    Manfred over 4 years
    You need to call Configure() before you can call Current to give it a context. However, the problem with that is that an HttpContext is different between threads. So, bottom line it appears as if this code is unlikely to work as intended.
  • Tom Stickel
    Tom Stickel over 4 years
    @Manfred - I would not have posted it unless it worked for me.
  • Manfred
    Manfred over 4 years
    @TomStickel No worries. That's fine. :-)
  • pim
    pim about 4 years
    Very very neat idea @TomStickel
  • prem
    prem over 2 years
    As everyone said, Current is null. How to call Configure to set it?
  • Tom Stickel
    Tom Stickel over 2 years
    @prem This worked in 2.x or maybe 1.x - that is when I wrote this answer. That is 2.5 years ago. I don't know at this point. sorry