C# Fiddler 测试 API Post 传递 [Frombody] 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19395669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:54:20  来源:igfitidea点击:

Fiddler testing API Post passing a [Frombody] class

c#asp.net-web-apifiddler

提问by SF Developer

I have this very simple C# APIController named "TestController" with an API method as:

我有一个名为“TestController”的非常简单的 C# APIController,其 API 方法为:

[HttpPost]
public string HelloWorld([FromBody] Testing t)
{
    return t.Name + " " + t.LastName;
}

Contact is just a class that look like this:

Contact 只是一个看起来像这样的类:

public class Testing
{
    [Required]
    public string Name;
    [Required]
    public string LastName;
}

My APIRouter looks like this:

我的 APIRouter 看起来像这样:

config.Routes.MapHttpRoute(
     name: "TestApi",
     routeTemplate: "api/{controller}/{action}/{id}",
     defaults: new { id = RouteParameter.Optional }
);

QUESTION 1:
How can I test that from a C# Client?

问题 1
如何从 C# 客户端进行测试?

For #2 I tried the following code:

对于#2,我尝试了以下代码:

private async Task TestAPI()
{
    var pairs = new List<KeyValuePair<string, string>> 
    {
       new KeyValuePair<string, string>("Name", "Happy"),
       new KeyValuePair<string, string>("LastName", "Developer")
    };

    var content = new FormUrlEncodedContent(pairs);

        var client = new HttpClient();                        
        client.DefaultRequestHeaders.Accept.Add(
             new MediaTypeWithQualityHeaderValue("application/json"));

        var result = await client.PostAsync( 
             new Uri("http://localhost:3471/api/test/helloworld", 
                    UriKind.Absolute), content);

        lblTestAPI.Text = result.ToString();
    }

QUESTION 2:
How can I test this from Fiddler?
Can't seem to find how to pass a Class via the UI.

问题 2
我如何从 Fiddler 测试这个?
似乎无法找到如何通过 UI 传递类。

采纳答案by Abhijeet Patel

For Question 1: I'd implement the POST from the .NET client as follows. Note you will need to add reference to the following assemblies: a) System.Net.Http b) System.Net.Http.Formatting

对于问题 1:我将按如下方式从 .NET 客户端实现 POST。请注意,您需要添加对以下程序集的引用:a) System.Net.Http b) System.Net.Http.Formatting

public static void Post(Testing testing)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:3471/");

        // Add an Accept header for JSON format.
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        // Create the JSON formatter.
        MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();

        // Use the JSON formatter to create the content of the request body.
        HttpContent content = new ObjectContent<Testing>(testing, jsonFormatter);

        // Send the request.
        var resp = client.PostAsync("api/test/helloworld", content).Result;

    }

I'd also rewrite the controller method as follows:

我还将重写控制器方法如下:

[HttpPost]
public string HelloWorld(Testing t)  //NOTE: You don't need [FromBody] here
{
  return t.Name + " " + t.LastName;
}

For Question 2: In Fiddler change the verb in the Dropdown from GET to POST and put in the JSON representation of the object in the Request body

对于问题 2:在 Fiddler 中,将下拉列表中的动词从 GET 更改为 POST,并在请求正文中放入对象的 JSON 表示

enter image description here

在此处输入图片说明