我正在使用以下标头和正文测试带有提琴手的 API,并在以下位置发布http://localhost:50063/api/image
:
User-Agent: Fiddler
Content-Type: application/json; charset=utf-8
Host: localhost:50063
Content-Length: 32330
{"filename": "bot.png", "file": "base64 image ellided for brevity"}
教程中的示例代码
[ApiController]
[Produces("application/json")]
[Route("api/Image")]
public class ImageController : Controller
{
// POST: api/image
[HttpPost]
public void Post(byte[] file, string filename)
{
string filePath = Path.Combine(_env.ContentRootPath, "wwwroot/images/upload", filename);
if (System.IO.File.Exists(filePath)) return;
System.IO.File.WriteAllBytes(filePath, file);
}
//...
}
首先,我收到错误 500,文件名为空。我将[ApiController]
Attribute 添加到控制器类,我得到错误400 filename invalid。
当我在这里提出相同的请求时,filename
绑定到复杂类:
[HttpPost("Profile")]
public void SaveProfile(ProfileViewModel model)
{
string filePath = Path.Combine(_env.ContentRootPath, "wwwroot/images/upload", model.FileName);
if (System.IO.File.Exists(model.FileName)) return;
System.IO.File.WriteAllBytes(filePath, model.File);
}
public class ProfileViewModel
{
public byte[] File { get; set; }
public string FileName { get; set; }
}
为什么会这样?