Search code examples
c#asp.net-core.net-core

The type or namespace name 'DefaultHttpRequest' could not be found


I have just upgraded to .net core 3.1 from 2.1.

I updated all the packages but i am getting the below errors which I am not able to resolve:

The type or namespace name 'DefaultHttpRequest' could not be found 
The type or namespace name 'Internal' does not exist in the namespace 'Microsoft.AspNetCore.Http'

I have been using above as:

using Microsoft.AspNetCore.Http.**Internal**;

 private static async Task<UserInfo>
            Uplpoad(byte[] buffer, FormFileCollection formFileCollection,
                **DefaultHttpRequest defaultHttpRequest,**
                Dictionary<string, StringValues> dictionary)
  {
   //some code
    defaultHttpRequest.Form = new FormCollection(dictionary, formFileCollection);
   //some code

   }

  public async void  Test1Up()
  { 
    //some code
    var defaultHttpContext = new DefaultHttpContext(featureCollection);
    var defaultHttpRequest = new **DefaultHttpRequest**(defaultHttpContext);
    //some code
  }

See the error in the highlighted lines in **

I can see in the breaking changes for 3.1 there are changes to DefaultHttpContext but I didn't find anything related to DefaultHttpRequest which is giving me errors as above.


Solution

  • DefaultHttpRequest was changed from public to internal as part of the ASP.NET Core 3.0 release, which means it's no longer available.

    However, in the code you've shown, there doesn't appear to be any reason to create or depend on DefaultHttpRequest. I recommend changing the code to something like this:

    private static async Task<UserInfo> Upload(byte[] buffer, FormFileCollection formFileCollection,
        HttpRequest httpRequest, Dictionary<string, StringValues> dictionary)
    {
        // ...
    
        httpRequest.Form = new FormCollection(dictionary, formFileCollection);
    
        // Update all other references to defaultHttpRequest
    
        // ...
    }
    
    public async void Test1Up() // async void is generally bad, but that's a separate issue
    { 
        // ...
    
        var httpContext = new DefaultHttpContext(featureCollection);
        var httpRequest = httpContext.Request;
    
        // Update all other references to defaultHttpContext and defaultHttpRequest
    
        // ...
    }
    

    Rather than passing around DefaultHttpRequest, use the abstract HttpRequest. The Form property being set in Upload is part of HttpRequest, so that should work as-is. There's also no need to create an instance of DefaultHttpRequest: the DefaultHttpContext constructor does this for you and provides it via its Request property.

    Here's the official announcement and relevant linked issue.