In .Net7 Web API doesn't return multipart content

Venkatajalapathy K 0 Reputation points
2023-08-03T13:55:08.2333333+00:00

I have tried to send file through multipart form content. The code in API like shown below:

app.MapGet("/GetFile", () =>
               {
                   MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
                   multiPartContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=----MyGreatBoundary");

                   HttpContent content1 = new ByteArrayContent(File.ReadAllBytes(@"C:/temp/TimeAkis.txt"));
                   content1.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("TimeAkis");
                   
                   content1.Headers.ContentDisposition.FileName = "TimeAkis.txt";
                   multiPartContent.Add(content1);

                   return multiPartContent;
               });


While accessing the above api method through a console application. We couldn't get the 'MultiPartContent'.

The code shown like below:

internal async Task Read()
{

HttpClient httpClient = new HttpClient();
            
            Uri webService = new Uri(@"https://localhost:44324/GetFile");
            HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, webService);
            requestMessage.Headers.ExpectContinue = false;

            Task<HttpResponseMessage> httpRequest = httpClient.GetAsync(webService);
            HttpResponseMessage responseMessage = httpRequest.GetAwaiter().GetResult();

            string strContentType = responseMessage.Content.Headers.ContentType?.MediaType;
            if(strContentType != null && IsMultipartContentType(strContentType))
            {
                try
                {
                    var multipartcontent = await responseMessage.Content.ReadAsMultipartAsync();
                    foreach(HttpContent content in multipartcontent.Contents)
                    {

                    }
                }
                catch(Exception e)
                {
                    Console.WriteLine(e);
                    
                }
        }

}

It returns the content type as 'Json':

image

Please help us to get the multipart data. (OR)

Am I wrong in creating API with multipart?.

Please suggest to me.

Thanks.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,500 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
326 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 63,501 Reputation points
    2023-08-03T14:43:22.2133333+00:00

    It’s uncommon for an api to return multipart, but it’s your api.

    your issue is that the object returned to the MapGet is json serialized unless it’s of type HttpResult. There is no builtin response type for multipart. The easiest for your code is probably to return a stream.

    https://video2.skills-academy.com/en-us/aspnet/core/fundamentals/minimal-apis/responses?view=aspnetcore-7.0

    note: if you will need more than once, you can create a custom HttpResult to handle multipart.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.