我有一个非常简单的 C# APIController,名为“TestController”,其 API 方法如下:
[HttpPost]
public string HelloWorld([FromBody] Testing t)
{
return t.Name + " " + t.LastName;
}
Contact 只是一个如下所示的类:
public class Testing
{
[Required]
public string Name;
[Required]
public string LastName;
}
我的 APIRouter 看起来像这样:
config.Routes.MapHttpRoute(
name: "TestApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
问题1:
如何从 C# 客户端测试它?
对于#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();
}
问题2:
我如何从 Fiddler 测试这个?
似乎无法找到如何通过 UI 传递类。