ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c#-如果内容为XML格式,如何在.NET Core 2.2中进行POST?

2019-12-11 04:07:15  阅读:553  来源: 互联网

标签:httpwebresponse asp-net-core net-core httpwebrequest c


我真的对这个问题感到惊讶,因为我记得在早期的.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” /&gt ;,则可以实现自定义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类对象填充值.

标签:httpwebresponse,asp-net-core,net-core,httpwebrequest,c
来源: https://codeday.me/bug/20191211/2106317.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有