Get
和Post
方法可以正常工作,但是当我尝试调用Delete
端点时,似乎从未执行过。
UserController.cs
[HttpDelete]
[MapToApiVersion("1.0")]
public async Task<IActionResult> Delete([FromForm] string userName)
{
return await RemoveUser(userName);
}
我正在使用HttpClient
来执行请求,如下所示:
using (Client = new HttpClient())
{
Client.BaseAddress = new Uri("https://localhost:44332/");
var result = await Client.DeleteAsync(new Uri($"/api/v{Version}/User" +"/xxx"));
return result.ToString();
}
我已经创建了一个控制台应用程序来测试API:
Program.cs
public class Program
{
private static readonly HttpClient Client = new HttpClient { BaseAddress = new Uri("https://localhost:44332/") };
public static void Main(string[] args)
{
Task.Run(() => RunAsync(args));
Console.ReadLine();
}
private static async Task RunAsync(IReadOnlyList<string> args)
{
var result = await Client.DeleteAsync(new Uri($"/api/v1/user/gareth"));
Console.WriteLine(result.ToString());
}
}
当我使用Postman调用相同的端点时,它起作用了,我在做什么错了?
答案 0 :(得分:1)
您正在尝试从请求正文(maven-jar-plugin:3.1.0
)解析用户名,但是您没有向HTTP客户端提供任何有效负载,而是在URL中指定参数。因此,您的API方法应如下所示:
UserController.cs
[FromBody]
下面的代码将针对[HttpDelete("{userName}")]
public async Task<IActionResult> Delete(string userName)
{
return await RemoveUser(userName);
}
发出DELETE
请求,并将UserController
作为john-doe
参数传递。
Program.cs
userName