状态 415 不支持的媒体类型表单数据
Status 415 Unsupported Media Type Form-Data
我正在尝试使用来自 Body 的表单数据从 PostMan 的 API 控制器调用 Post 方法。但是从邮递员点击发送按钮后,显示错误消息“不支持的媒体类型”
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "|cd267e9d-4f557653ef6391ef."
}
这是代码。
型号 class
public class PersonCreationDTO
{
[Required]
[StringLength(120)]
public string PersonName { get; set; }
public string Biography { get; set; }
public DateTime DateOfBirth { get; set; }
public IFormFile Picture { get; set; }
}
public class Person
{
public int Id { get; set; }
[Required]
[StringLength(120)]
public string PersonName { get; set; }
public string Biography { get; set; }
public DateTime DateOfBirth { get; set; }
public string Picture { get; set; }
}
控制器
[ApiController]
[Route("api/people")]
public class PeopleController:ControllerBase
{
private readonly ApplicationDbContext context;
private readonly IMapper mapper;
public PeopleController(ApplicationDbContext context, IMapper mapper)
{
this.context = context;
this.mapper = mapper;
}
[HttpPost]
public async Task<ActionResult> Post([FromBody] PersonCreationDTO personCreation)
{
var person = mapper.Map<Person>(personCreation);
context.Add(person);
//await context.SaveChangesAsync();
//var personDTO = mapper.Map<PersonDTO>(person);
//return new CreatedAtRouteResult("getPerson", new { Id = personDTO.Id }, personDTO);
return NoContent();
}
}
添加 [FromForm]
而不是 [FromBody]
[HttpPost]
public async Task<ActionResult> Post([FromForm] PersonCreationDTO personCreation)
{
//clarify code
}
在 Postman 中单击 headers 并单击 (select) 第一个内容类型 - multipart/form-data;边界=发送请求时计算
在您的控制器操作中,将 [FromBody] 替换为 [FromForm]
我正在尝试使用来自 Body 的表单数据从 PostMan 的 API 控制器调用 Post 方法。但是从邮递员点击发送按钮后,显示错误消息“不支持的媒体类型”
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "|cd267e9d-4f557653ef6391ef."
}
这是代码。 型号 class
public class PersonCreationDTO
{
[Required]
[StringLength(120)]
public string PersonName { get; set; }
public string Biography { get; set; }
public DateTime DateOfBirth { get; set; }
public IFormFile Picture { get; set; }
}
public class Person
{
public int Id { get; set; }
[Required]
[StringLength(120)]
public string PersonName { get; set; }
public string Biography { get; set; }
public DateTime DateOfBirth { get; set; }
public string Picture { get; set; }
}
控制器
[ApiController]
[Route("api/people")]
public class PeopleController:ControllerBase
{
private readonly ApplicationDbContext context;
private readonly IMapper mapper;
public PeopleController(ApplicationDbContext context, IMapper mapper)
{
this.context = context;
this.mapper = mapper;
}
[HttpPost]
public async Task<ActionResult> Post([FromBody] PersonCreationDTO personCreation)
{
var person = mapper.Map<Person>(personCreation);
context.Add(person);
//await context.SaveChangesAsync();
//var personDTO = mapper.Map<PersonDTO>(person);
//return new CreatedAtRouteResult("getPerson", new { Id = personDTO.Id }, personDTO);
return NoContent();
}
}
添加 [FromForm]
而不是 [FromBody]
[HttpPost]
public async Task<ActionResult> Post([FromForm] PersonCreationDTO personCreation)
{
//clarify code
}
在 Postman 中单击 headers 并单击 (select) 第一个内容类型 - multipart/form-data;边界=发送请求时计算
在您的控制器操作中,将 [FromBody] 替换为 [FromForm]