我真的对这个问题感到惊讶,因为我记得在早期的.NET Core版本中已经成功了.我正在开发.NET Core 2.2应用程序,现在需要由只能发布xml的另一个应用程序(由外部开发)调用.
这是我的ConfigureServices方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddXmlSerializerFormatters();
}
这是我的控制器:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// POST api/values
[HttpPost]
public ActionResult<object> Post([FromBody] object value)
{
return ("Hi", "Hi2");
}
}
以下请求的确会引起状态码为200的响应:
POST http://localhost:58774/api/values
Content-Type: application/json
Accept: application/xml
User-Agent: vscode-restclient
Accept-Encoding: gzip
{"a":5}
这给了我xml作为响应,并且
POST http://localhost:58774/api/values
Content-Type: application/json
Accept: application/json
User-Agent: vscode-restclient
Accept-Encoding: gzip
{"a":5}
给出json作为响应.
但是,此调用导致状态码为500的响应(这正是我的问题):
POST http://localhost:58774/api/values
Content-Type: application/xml
Accept: application/xml
User-Agent: vscode-restclient
Accept-Encoding: gzip
<A a="5"/>
所以现在我有麻烦了. Xml格式可以正常工作,如果我接受它作为输出类型,它将变得很清晰.但是,如果我自己将其发布为Content-Type并进行测试,则得到500.我也尝试了this (old) approach,但它似乎在.NET Core 2.2中不起作用.我究竟做错了什么?如何在.net core 2.2中发布xml?
有用的评论后的更新.
导致500的异常是这样的:
System.InvalidOperationException: There is an error in XML document
(1, 11). —> System.InvalidOperationException: was not
expected.
但是,如果我添加一个xmlns(基于this),我仍然有500:
POST http://localhost:5000/api/values
Content-Type: application/xml
Accept: application/xml
User-Agent: vscode-restclient
Accept-Encoding: gzip
<f:table xmlns:f="https://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
然后,异常消息是:
System.InvalidOperationException: There is an error in XML document
(1, 56). —> System.InvalidOperationException: https://www.w3schools.com/furniture’> was not expected.
可能我需要更改xml.怎么样?甚至w3cschools的示例也无济于事.
解决方法:
对于Content-Type:带有{“ a”:5}的application / json,您将在服务器端收到{“ a”:5}.它收到纯文本.
对于Content-Type:带有< A a =“ 5” />的application / xml,如果您希望接收< A a =“ 5” /> ;,则可以实现自定义XDocumentInputFormatter,例如
public class XDocumentInputFormatter : InputFormatter, IInputFormatter, IApiRequestFormatMetadataProvider
{
public XDocumentInputFormatter()
{
SupportedMediaTypes.Add("application/xml");
}
protected override bool CanReadType(Type type)
{
if (type.IsAssignableFrom(typeof(XDocument))) return true;
return base.CanReadType(type);
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
var xmlDoc = await XDocument.LoadAsync(context.HttpContext.Request.Body, LoadOptions.None, CancellationToken.None);
return InputFormatterResult.Success(xmlDoc);
}
}
在Startup.cs中注册
services.AddMvc(config =>
{
config.InputFormatters.Insert(0, new XDocumentInputFormatter());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddXmlSerializerFormatters();
默认情况下,对于XmlSerializer,我们需要提供Type类型,而对象类型将无法反序列化.
如果对象值的类型定义为
public class A
{
public int a { get; set; }
}
您可以像这样更改您的方法
public ActionResult<object> Post([FromBody] A value)
{
return new A { a = 1 };//("Hi", "Hi2");
}
和像这样的要求
<A>
<a>1</a>
</A>
它将用A类对象填充值.