asp.net-mvc – 使用Multipart格式的Web API模型绑定
有没有办法能够从ASP.NET MVC Web API中的多部分表单数据请求中获取模型绑定(或任何)来发布模型?
我看到各种博客文章,但事情发生在实际版本和实际版本之间,或者它们不显示模型绑定工作。 这是一个过时的帖子:Sending HTML Form Data 这也是这样的:Asynchronous File Upload using ASP.NET Web API 我发现这个代码(并修改了一些)在某个地方手动读取值: 模型: public class TestModel { [Required] public byte[] Stream { get; set; } [Required] public string MimeType { get; set; } } 控制器: public HttpResponseMessage Post() { if (!Request.Content.IsMimeMultipartContent("form-data")) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } IEnumerable<HttpContent> parts = Request.Content.ReadAsMultipartAsync().Result.Contents; string mimeType; if (!parts.TryGetFormFieldValue("mimeType",out mimeType)) { return Request.CreateResponse(HttpStatusCode.BadRequest); } var media = parts.ToArray()[1].ReadAsByteArrayAsync().Result; // create the model here var model = new TestModel() { MimeType = mimeType,Stream = media }; // save the model or do something with it // repository.Save(model) return Request.CreateResponse(HttpStatusCode.OK); } 测试: [DeploymentItem("test_sound.aac")] [TestMethod] public void CanPostMultiPartData() { var content = new MultipartFormDataContent { { new StringContent("audio/aac"),"mimeType"},new ByteArrayContent(File.ReadAllBytes("test_sound.aac")) }; this.controller.Request = new HttpRequestMessage {Content = content}; var response = this.controller.Post(); Assert.AreEqual(response.StatusCode,HttpStatusCode.OK); } 这个代码基本上是脆弱的,不可维护的,进一步的,不强制模型绑定或数据注释约束。 有没有更好的方法来做到这一点? 更新:我看过这个post,这让我想起来 – 我必须为每个我想支持的模型编写一个新的格式化程序? 解决方法
@Mark琼斯链接到我的博客文章
http://lonetechie.com/2012/09/23/web-api-generic-mediatypeformatter-for-file-upload/,带领我来到这里。我必须考虑如何做你想做的事情。
我相信如果你将我的方法与TryValidateProperty()结合起来,你应该可以完成你所需要的。我的方法会得到反序列化的对象,但是它不会处理任何验证。您需要使用反射来循环遍历对象的属性,然后在每个对象上手动调用TryValidateProperty()。这种方法是多一点手,但我不知道如何做到这一点。 http://msdn.microsoft.com/en-us/library/dd382181.aspx 编辑:有人问这个问题,我决定编写代码,以确保它的工作。这是我的博客中更新的验证检查代码。 public class FileUpload<T> { private readonly string _RawValue; public T Value { get; set; } public string FileName { get; set; } public string MediaType { get; set; } public byte[] Buffer { get; set; } public List<ValidationResult> ValidationResults = new List<ValidationResult>(); public FileUpload(byte[] buffer,string mediaType,string fileName,string value) { Buffer = buffer; MediaType = mediaType; FileName = fileName.Replace(""",""); _RawValue = value; Value = JsonConvert.DeserializeObject<T>(_RawValue); foreach (PropertyInfo Property in Value.GetType().GetProperties()) { var Results = new List<ValidationResult>(); Validator.TryValidateProperty(Property.GetValue(Value),new ValidationContext(Value) {MemberName = Property.Name},Results); ValidationResults.AddRange(Results); } } public void Save(string path,int userId) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var SafeFileName = Md5Hash.GetSaltedFileName(userId,FileName); var NewPath = Path.Combine(path,SafeFileName); if (File.Exists(NewPath)) { File.Delete(NewPath); } File.WriteAllBytes(NewPath,Buffer); var Property = Value.GetType().GetProperty("FileName"); Property.SetValue(Value,SafeFileName,null); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-4 – 使用Bootstrap创建数据验证错误
- Asp.net MVC 3:控制器中的模型是空的吗?
- asp.net-mvc – DefaultModelBinder不绑定嵌套模
- asp.net-mvc-4 – MVC 4创建slug类型url
- 在Docker上部署自动更新ssl证书的nginx + .NET C
- asp.net-mvc – 可以在mvc JsonResult控制器方法
- “DataSource和DataSourceID都被定义为”使用ASP
- asp.net-mvc – ASP.NET MVC – HtmlHelper和Ses
- asp.net – IIS 8.0中内核模式和用户模式缓存之间
- asp.net – SESSION VARIABLES的替代品是什么?