简体   繁体   English

ASP.NET 核心 2 - 缺少内容类型边界

[英]ASP.NET Core 2 - Missing content-type boundary

I'm trying to upload a file from a Angular client to my ASP.NET Core 2 WebAPI service.我正在尝试将文件从 Angular 客户端上传到我的 ASP.NET Core 2 WebAPI 服务。 When I call the service, I get back an Internal Server Error.当我调用该服务时,我得到一个内部服务器错误。 That's the error I'm getting:这就是我得到的错误: 在此处输入图像描述

The component I'm using client-side is this one: ngx-uploader我在客户端使用的组件是这个: ngx-uploader

In my request options, i set them as you can see here:在我的请求选项中,我将它们设置为您可以在此处看到的:

  const event: UploadInput = {
  type: 'uploadAll',
  url: this.printService.apiFilesBaseUrl + '/Upload',
  method: 'POST',
  file: this.files[0],
  headers: {
    'Content-Type': 'multipart/form-data',
    'Accept': '*/*',
    'Authorization': 'Bearer ' + this.authService.getToken()
  }
};

While, server-side my controller action signature is this one:同时,服务器端我的 controller 动作签名是这个:

    [HttpPost]
    [Route("Upload")]
    [Authorize]
    public Guid Post(IFormFile file)

Breakpoints in this controller action never get hit.此 controller 操作中的断点永远不会被击中。

Can someone please share ideas about what's happening here?有人可以分享一下这里发生的事情吗?

Here, as requested, i will post my request header and payload:在这里,根据要求,我将发布我的请求 header 和有效负载:

在此处输入图像描述

Thanks in advance.提前致谢。

检查开发者工具网络选项卡中的上传请求,它应该具有正确的格式(匹配'Content-Type': 'multipart/form-data' ),您也可以尝试删除此标头。

You can add content-type: as multipart/form-data;您可以添加content-type: as multipart/form-data; boundary=--14737809831466499882746641449 .边界=--14737809831466499882746641449

I am testing API on postman and added content-type like above it worked for me.我正在 postman 上测试 API 并添加了类似上面的内容类型,它对我有用。

For anyone having similar problem in netcore 3.1 or net 6.0, please make sure You have the Consumes attribute, ie:对于在 netcore 3.1 或 net 6.0 中遇到类似问题的任何人,请确保您具有Consumes属性,即:

[HttpPost]
[Consumes("multipart/form-data")]
public async Task<IActionResult> ImportItems(IFormFile file)
{
   //...
}

On Angular 14, if you set the header like this:在 Angular 14 上,如果您像这样设置 header:

const httpOptions = { headers: new HttpHeaders().set('Content-Type', 'multipart/form-data')
};

then you should remove this header so Web Browser will add this Content-Type header itself, ie:那么你应该删除这个 header 所以 Web 浏览器将添加这个Content-Type header 本身,即:

multipart/form-data;多部分/表单数据; boundary=----WebKitFormBoundaryAGc4qL8mDa483NA2边界=----WebKitFormBoundaryAGc4qL8mDa483NA2

Also make sure that you add enctype attribute of html form element with value multipart/form-data , ie:还要确保添加 html 表单元素的enctype属性,其值为multipart/form-data ,即:

<form #htmlForm method="post" action="#" enctype="multipart/form-data" (submit)="onFormSubmit($event)">

and decorate your asp.net core web api action with Consumes attribute, ie:并使用Consumes属性装饰您的 asp.net 核心 web api 动作,即:

[HttpPost("version")]                                     
[Consumes("multipart/form-data")]          
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> CreateAppVersion()
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidOperationException:错误的内容类型:ASP.NET Core - InvalidOperationException: Incorrect Content-Type: ASP.NET Core C#缺少内容类型边界 - C# Missing content-type boundary ASP.Net Core 为 JSON 内容设置 Content-Type 和 Location 标头 - ASP.Net Core set Content-Type and Location headers for JSON content ASP.NET Core 中内容类型“application/csp-report”的“415 Unsupported Media Type” - "415 Unsupported Media Type" for Content-Type "application/csp-report" in ASP.NET Core 尝试使用Ajax将png上载到Asp.net Core服务器时出现“错误的Content-Type:image / png” - “Incorrect Content-Type: image/png” when trying to upload png with Ajax to Asp.net Core server ASP.NET 核心 jQuery POST - 尽管标题格式正确,但内容类型不正确 - ASP.NET Core jQuery POST - Incorrect Content-Type despite correctly formatted headers 如何在 ASP.NET Core MVC controller 中为重定向设置 Content-Type - How to set Content-Type for a Redirect in ASP.NET Core MVC controller ASP.NET MVC应用程序不输出内容类型标头 - ASP.NET MVC Application not outputting content-type header POST请求标头中的content-type缺少FormData边界 - FormData boundary missing from content-type in POST request header 如果我的 Z099E7396E 中根本没有 Content-Type,ASP.NET 核心 Web API 管道的流程是什么? - What is the flow of ASP.NET Core Web API pipeline if I do not have Content-Type at all in my header?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM