【发布时间】:2014-03-24 19:04:15
【问题描述】:
我正在从 web broser rest 客户端发送 Xml 文件。我需要在 asp.net web api http post 方法中接受 xml 文件。
如何从 asp.net web api http post 方法获取 xml 文件内容、文件名和标题内容?
我参考了一些msdn链接,例如http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2,我没有得到这个教程
不知怎么写了代码
HttpRequestMessage request = this.Request;
var task = this.Request.Content.ReadAsStreamAsync();
task.Wait();
Stream requestStream = task.Result;
string inp = request.Content.ReadAsStringAsync().Result;
string result = await request.Content.ReadAsStringAsync();
try
{
Stream fileStream = File.Create(@"c:\\test\\1.xml");
requestStream.CopyTo(fileStream);
fileStream.Close();
requestStream.Close();
}
catch (IOException)
{
throw new HttpResponseException("A generic error occured. Please try again later.", HttpStatusCode.InternalServerError);
}
通过上面的代码,我没有得到完整的 xml 内容。
我对 asp.net web api 和 .net 框架完全陌生。 请提供实现这一点的过程和代码。
【问题讨论】:
-
如何发送?作为文件上传?在 POST 中?你是什么意思“没有得到完整的xml内容”?少了什么东西?你得到了什么?
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
我通过http post文件上传发送
标签: c# asp.net asp.net-mvc-4 asp.net-web-api asp.net-web-api2