【发布时间】:2018-01-29 11:02:48
【问题描述】:
假设我们有一个 html 表单,它可以发布带有产品名称和描述的产品图片。
产品名称 : 描述 : 图像文件我想在不使用提供者表单数据的情况下检索“ProductName”和“Description”字段值。
Web API 控制器 --
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// Show all the key-value pairs.
foreach (var key in provider.FormData.AllKeys)
{
foreach (var val in provider.FormData.GetValues(key))
{
Trace.WriteLine(string.Format("{0}: {1}", key, val));
}
}
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
我们可以轻松获取所有属性值。但是我们可以做一个如下的 web Api 函数吗:
[HttpPost]
public IHttpActionResult Upload(ProductInfo model){
return Content(HttpStatusCode.OK, true);
}
Public Class ProductInfo {
public string ProductName{ get; set; }
public string Description{ get; set; }
}
【问题讨论】:
标签: c# asp.net asp.net-mvc-4 asp.net-web-api asp.net-web-api2