我有一个名为“TestController”的非常简单的 C# APIController,其 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 传递一个类。