![](/img/trans.png)
[英]Get custom validation error message from Web Api request to HttpClient
[英]HttpClient not sending the request to Web Api async get method
我已经有一段时间没有使用Web Api了,但我坚持使用应该很简单的方法。
我在Web Api控制器中有一个异步get方法:
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);
}
我遇到的问题是,由于我在MyMethodAsync上设置的断点发出警告,提示您符号未加载,似乎请求甚至没有发送到Web Api。
真正令我困惑的是,下一个调用是在同一控制器上的POST方法,当我调用它时,符号加载并且其断点被命中。
就像我说的那样,经过很长时间之后,我又回到了Web Api,很多事情在我的脑海中变得模糊。 我只有默认路由,所以我不知道是否必须添加更多特定的路由,如果要添加,则如何指定它们。
附带说明一下,我还有另外两个具有Guid参数的GET方法。 我试过给那些结果相同的人打电话。
谢谢您的帮助。
更新 : 使事情更加混乱。 如果我运行该应用程序,但没有使用客户端,而是打开浏览器,并完全按照客户端的方式输入URL,则操作中的断点会被命中。 在那之后,我尝试使用客户端,然后就没有了。
config.Routes.MapHttpRoute(
name: "NewRoute",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
[ActionName("MyMethod1")]
[HttpGet]
public IHttpActionResult MyMethod1(Guid id)
{
return Ok<string>($" ok MyMethod1 and id= {id}");
}
[ActionName("MyMethod2")]
[HttpGet]
public IHttpActionResult MyMethod2(Guid id)
{
return Ok<string>($" ok MyMethod2 and id= {id}");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.