简体   繁体   English

C#Asp.net Web API-创建JSON / XML转换器API

[英]C# Asp.net Web API - creating a JSON/XML converter API

Just as an exercise I'd like to create an ASP.Net web API which converts between XML and JSON. 作为练习,我想创建一个在XML和JSON之间转换的ASP.Net Web API。

this is what I have so far: 这是我到目前为止所拥有的:

在此处输入图片说明

I think the content type checking is right, but I can't test as I am getting "Method not Allowed" when I try and post, is it possible to have routing/binding to achieve my goal? 我认为内容类型检查是正确的,但是我无法测试,因为当我尝试发布时收到“方法不允许”,是否可以进行路由/绑定来实现我的目标?

I am trying to test this with Fiddler: 我正在尝试使用Fiddler进行测试: 在此处输入图片说明

my routes look like this: 我的路线如下所示: 在此处输入图片说明

my webapi routes look like this: 我的webapi路由如下所示: 在此处输入图片说明 but I still get Method Not Allowed :=/ 但是我仍然得到方法不允许:= / 在此处输入图片说明

Try annotating your action parameter with FromBodyAttribute . 尝试使用FromBodyAttribute注释您的action参数。 Here's an example: 这是一个例子:

public class TestController : ApiController
{
  public string Post([FromBody] string jsonOrXml)
  {
    // Process the input
  }
}

In this case the Content-Type request header must be application/x-www-form-urlencoded and the body will have the following format: =[JSON or XML data] . 在这种情况下, Content-Type请求标头必须为application/x-www-form-urlencoded ,并且正文将具有以下格式: =[JSON or XML data] More details are given here . 这里给出更多细节。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM