HttpClient 未将请求发送到 Web Api async get 方法
HttpClient not sending the request to Web Api async get method
我有一段时间没有使用 Web Api,但我被一些本应非常简单的东西卡住了。
我在 Web Api 控制器中有一个异步获取方法:
public class SomeController : ApiController
{
[HttpGet]
public async Task<IHttpActionResult> MyMethodAsync(Guid id)
{
//... doing something.
}
}
在我的客户端应用程序中,我尝试这样调用它:
using(var client = new HttpClient())
{
var result = client.GetAsync($"{url}/Some/MyMethodAsync/{Guid.NewGuid()}");
//I have tried making the above call with and without await
//since I saw in some other post that it is not needed.
//.... doing something else.
//This POST method does stop in the beak point I set inside of it.
var result2 = client.PostAsync($"{url}/Some/MyPostAsync", someContent);
}
我遇到的问题是,似乎请求甚至没有被发送到 Web Api,因为我在 MyMethodAsync 上设置的断点警告说符号没有被加载.
真正让我感到困惑的是,下一次调用是对同一控制器上的 POST 方法的调用,当我调用它时,符号加载并且它的断点被击中。
就像我说的,我在很长一段时间后重新回到 Web Api 并且很多事情在我脑海中模糊不清。我只有默认路由所以,我不知道我是否必须添加更具体的路由,如果需要,如何指定它们。
附带说明一下,我还有另外两个 GET 方法也有一个 Guid 参数。我试过打电话给那些结果相同的人。
感谢您的帮助。
更新:让事情变得更加混乱。如果我 运行 应用程序而不是使用客户端,我打开浏览器并完全按照客户端的方式输入 url,则操作中的断点被击中。在那之后我尝试使用客户端然后它不会。
1。打开App_Start/WebApiConfig.cs
2。添加新路线
config.Routes.MapHttpRoute(
name: "NewRoute",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
2.1。完整代码
3。使用操作名称属性 [ActionName("MyMethod1")]
[ActionName("MyMethod1")]
[HttpGet]
public IHttpActionResult MyMethod1(Guid id)
{
return Ok<string>($" ok MyMethod1 and id= {id}");
}
4。与 MyMethod2
相同
[ActionName("MyMethod2")]
[HttpGet]
public IHttpActionResult MyMethod2(Guid id)
{
return Ok<string>($" ok MyMethod2 and id= {id}");
}
额外注意:visual studio
中的符号未被删除错误
- 关闭visual studio
- 删除WebApplication1\bin
- 删除WebApplication1\obj
- 打开visual studio
- 构建解决方案
我有一段时间没有使用 Web Api,但我被一些本应非常简单的东西卡住了。
我在 Web Api 控制器中有一个异步获取方法:
public class SomeController : ApiController
{
[HttpGet]
public async Task<IHttpActionResult> MyMethodAsync(Guid id)
{
//... doing something.
}
}
在我的客户端应用程序中,我尝试这样调用它:
using(var client = new HttpClient())
{
var result = client.GetAsync($"{url}/Some/MyMethodAsync/{Guid.NewGuid()}");
//I have tried making the above call with and without await
//since I saw in some other post that it is not needed.
//.... doing something else.
//This POST method does stop in the beak point I set inside of it.
var result2 = client.PostAsync($"{url}/Some/MyPostAsync", someContent);
}
我遇到的问题是,似乎请求甚至没有被发送到 Web Api,因为我在 MyMethodAsync 上设置的断点警告说符号没有被加载.
真正让我感到困惑的是,下一次调用是对同一控制器上的 POST 方法的调用,当我调用它时,符号加载并且它的断点被击中。
就像我说的,我在很长一段时间后重新回到 Web Api 并且很多事情在我脑海中模糊不清。我只有默认路由所以,我不知道我是否必须添加更具体的路由,如果需要,如何指定它们。
附带说明一下,我还有另外两个 GET 方法也有一个 Guid 参数。我试过打电话给那些结果相同的人。
感谢您的帮助。
更新:让事情变得更加混乱。如果我 运行 应用程序而不是使用客户端,我打开浏览器并完全按照客户端的方式输入 url,则操作中的断点被击中。在那之后我尝试使用客户端然后它不会。
1。打开App_Start/WebApiConfig.cs
2。添加新路线
config.Routes.MapHttpRoute(
name: "NewRoute",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
2.1。完整代码
3。使用操作名称属性 [ActionName("MyMethod1")]
[ActionName("MyMethod1")]
[HttpGet]
public IHttpActionResult MyMethod1(Guid id)
{
return Ok<string>($" ok MyMethod1 and id= {id}");
}
4。与 MyMethod2
相同 [ActionName("MyMethod2")]
[HttpGet]
public IHttpActionResult MyMethod2(Guid id)
{
return Ok<string>($" ok MyMethod2 and id= {id}");
}
额外注意:visual studio
中的符号未被删除错误- 关闭visual studio
- 删除WebApplication1\bin
- 删除WebApplication1\obj
- 打开visual studio
- 构建解决方案