我在asp.net mvc中有一个api:
public class UploadController : Controller
{
...
public ActionResult Post(UploadModel uploadModel)
我希望从测试客户端调用此api,附加文件。
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
var byteArrayContent = new ByteArrayContent(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
byteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse("text/csv");
var uploadModel = new UploadModel { Allergene = "Anders", Description = "Desc", Email = "and@juu.com" };
var content = new StringContent(JsonConvert.SerializeObject(uploadModel), Encoding.UTF8, "application/json");
var multipartFormDataContent = new MultipartFormDataContent
{
{content},
{byteArrayContent, "\"file\"", "\"feedback.csv\""}
};
var result = httpClient.PostAsync("http://localhost:53411/Upload/Post", multipartFormDataContent).Result;
}
如果我发布multipartFormDataContent,我不会在我的模型上获得属性。如果我发布内容,我会获得属性,但显然不是附件。
我错过了什么?