我在前端使用React,我有一个带文本输入和一个文件输入的简单表单(我使用FormData对象)。
我使用axios发送请求。
请求已正确发送(已在Chrome调试控制台中检查)。 发送表格:
> Accept:application/json, text/plain, */* Authorization:Bearer
> eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjAzODMxMjUsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDAwMC8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjQwMDAvIn0.GfvFb4am3QxBdUgPIQX8OHLcg9G7vgovF4vwny-HiLg
> Content-Type:multipart/form-data;
> boundary=----WebKitFormBoundaryfuFU2JWOnbvNnK6L
> Origin:http://localhost:3000 Referer:http://localhost:3000/news
> User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2)
> AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186
> Safari/537.36
> ------WebKitFormBoundaryfuFU2JWOnbvNnK6L Content-Disposition: form-data; name="Title"
>
> h
> ------WebKitFormBoundaryfuFU2JWOnbvNnK6L Content-Disposition: form-data; name="BackgroundImg"; filename="Package assignment3.png"
> Content-Type: image/png
>
>
> ------WebKitFormBoundaryfuFU2JWOnbvNnK6L Content-Disposition: form-data; name="users_id"
>
> 5
> ------WebKitFormBoundaryfuFU2JWOnbvNnK6L Content-Disposition: form-data; name="Content"
>
> <p>jhl</p>
> ------WebKitFormBoundaryfuFU2JWOnbvNnK6L--
在服务器端,我有这个端点:
[HttpPost]
[Authorize]
[Route("{id}/news")]
public IActionResult CreateNewsByUserId(int id, [FromForm]NewsViewModel newsViewModel)
{
return Ok(newsViewModel);
}
ViewModel:
public class NewsViewModel
{
public string Title;
public string Content;
public IFormFile BackgroundImg;
}
newsViewModel
为空。
但是,我可以通过Request.Form
Object获取表单数据,或者如果我重写端点:
public IActionResult CreateNewsByUserId(int id, string Title, string Content, IFormFile BackgroundImg)
为什么newsViewModel为空?
谢谢