简体   繁体   中英

asp.net core 2.2 posting file results in http/415 unsopported mediatype

From postman I'm trying to post a file to an asp.net core 2.2 api-controller.

POST /api/epd/noshow/uploadfile HTTP/1.1
Host: localhost:44361
User-Agent: PostmanRuntime/7.11.0
Accept: */*
Cache-Control: no-cache
Postman-Token: 3d633228-2554-442b-84f4-4e8214972886,a8f994f4-973e-411f-b96b-778af2c082b4
Host: localhost:44361
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------201375848683546790642901
content-length: 237
Connection: keep-alive
cache-control: no-cache
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW


Content-Disposition: form-data; name="file"; filename="C:\temp\caldr.json


------WebKitFormBoundary7MA4YWxkTrZu0gW--

The api-controller method

[HttpPost]
[AllowAnonymous]
public IActionResult UploadFile([FromForm]IFormFile file)
{
    return Ok();
}

I've already tried FromForm, FromBody, nothing. Setting content-type in postman, not setting the content-type in postman always returns the same error.

Any suggestions ?

That is simple than expected :) You to send the request body in a form data. Here, this example shows only the simple file post call with ASp.NET Core 2.2 - WebAapi

Postman:

在此处输入图片说明

API:

在此处输入图片说明

Found the problem and solution. Just for reference if someone else encounters the same mind boggling problem.

On the 'grandmother' class of the controller, there is the following attribute :

[Consumes("application/json")]

of course form-data was not accepted :-)

override this default behaviour by adding the following attribute to the action

[Consumes("multipart/form-data")]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM