Asp.net mvc 如何在MVC4WebAPI中接收XmlDocument?
我正在将XmlDocument发布到ApiController(从windows服务,服务运行良好,发布正确,我在wcf web api中使用了它),但xml总是空的,我做错了什么? 我可以发布一些课程,比如教程,或者获取任何数据,一切都会好起来,但我不能发布XmlDocumentAsp.net mvc 如何在MVC4WebAPI中接收XmlDocument?,asp.net-mvc,api,web,xmldocument,Asp.net Mvc,Api,Web,Xmldocument,我正在将XmlDocument发布到ApiController(从windows服务,服务运行良好,发布正确,我在wcf web api中使用了它),但xml总是空的,我做错了什么? 我可以发布一些课程,比如教程,或者获取任何数据,一切都会好起来,但我不能发布XmlDocument public class XmlController : ApiController { public void PostXml(XmlDocument xml) { // code
public class XmlController : ApiController
{
public void PostXml(XmlDocument xml)
{
// code
}
}
PostXml
应该是控制器上的操作吗?如果是这样,您应该将控制器操作标记为接受HttpPost。在此基础上,我将对操作进行如下修改:
[HttpPost]
public ActionResult PostXml(HttpPostedFileBase xml)
{
// code
}
如果您仍然无法接受发布的文件,请启动调试器并检查请求文件集合:我找到了一个解决方案:
我们需要使用继承来继承MediaTypeFormatter
public class XmlMediaTypeFormatter : MediaTypeFormatter
{
public XmlMediaTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
}
public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, Stream stream,
HttpContentHeaders contentHeaders,
IFormatterLogger formatterLogger)
{
var taskCompletionSource = new TaskCompletionSource<object>();
try
{
var memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
var s = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(s);
taskCompletionSource.SetResult(xmlDoc);
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
}
return taskCompletionSource.Task;
}
public override bool CanReadType(Type type)
{
return type == typeof(XmlDocument);
}
public override bool CanWriteType(Type type)
{
return false;
}
}
控制器:
public HttpResponseMessage PostXml([FromBody] XmlDocument xml)
{//code...}
public HttpResponseMessage Post(XDocument xml)
{
return Request.CreateResponse(HttpStatusCode.OK, xml);
}
我遵循@Rhot给出的解决方案,但不知何故它不起作用,所以我编辑如下内容,这对我来说是可行的:
public class XmlMediaTypeFormatter : MediaTypeFormatter
{
public XmlMediaTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
}
public override bool CanReadType(Type type)
{
return type == typeof(XDocument);
}
public override bool CanWriteType(Type type)
{
return type == typeof(XDocument);
}
public override Task<object> ReadFromStreamAsync(Type type, Stream stream, HttpContent content, IFormatterLogger formatterLogger)
{
var reader = new StreamReader(stream);
string value = reader.ReadToEnd();
var tcs = new TaskCompletionSource<object>();
try
{
var xmlDoc = XDocument.Parse(value);
tcs.SetResult(xmlDoc);
}
catch (Exception ex)
{
//disable the exception and create custome error
//tcs.SetException(ex);
var xml = new XDocument(
new XElement("Error",
new XElement("Message", "An error has occurred."),
new XElement("ExceptionMessage", ex.Message)
));
tcs.SetResult(xml);
}
return tcs.Task;
}
public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContent content, TransportContext transportContext)
{
var writer = new StreamWriter(stream);
writer.Write(((XDocument)value).ToString());
writer.Flush();
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(null);
return tcs.Task;
}
}
在我的WebAPI控制器下面:
public HttpResponseMessage PostXml([FromBody] XmlDocument xml)
{//code...}
public HttpResponseMessage Post(XDocument xml)
{
return Request.CreateResponse(HttpStatusCode.OK, xml);
}
它不是mvc控制器,而是ApicController,接下来就没有必要添加[HttpPost]属性。此外,在您的示例中,xml也是空的。