How to replace Microsoft.AspNetCore.Http – C#

by
Ali Hasan
.net-6.0 asp.net-core-6.0 c#

Quick Fix: To use ASP.NET Core parts in your class library, add <FrameworkReference Include="Microsoft.AspNetCore.App" /> to your project file .csproj.

The Problem:

Before, I employed Microsoft.AspNetCore.Http for handling HTTP requests. It’s since been deprecated, rendering HttpContext unavailable, so what can I use as an alternative to handle requests effectively?

The Solutions:

Solution 1: Use ASP.NET Core parts in class library

If you want to use ASP.NET Core parts in class library, you need to add <FrameworkReference Include="Microsoft.AspNetCore.App"/> to library projects .csproj file as explained in the [docs][1]:

&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
  &lt;!--... rest of file--&gt;

  &lt;ItemGroup&gt;
    &lt;FrameworkReference Include=&quot;Microsoft.AspNetCore.App&quot; /&gt;
  &lt;/ItemGroup&gt;

  &lt;!--... rest of file--&gt;
&lt;/Project&gt;

Q&A

What package should be added to csproj to allow access to HttpContext?

Add <FrameworkReference Include="Microsoft.AspNetCore.App" />.

What is the alternative to HttpContext in ASP.NET Core?

Add Microsoft.AspNetCore.App framework reference to use HttpContext in library project.

Video Explanation:

The following video, titled "How to Create a Web API with ASP.NET CORE and .NET 6 (c# for ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... c/TechWithPat?sub_confirmation=1 TIMESTAMPS : 00:00 Introduction 00:19 What is a Web API ? 03:30 Creating a Rest API with ASP.NET Core 07:26 ...