简体   繁体   English

HttpClient未将请求发送到Web Api异步get方法

[英]HttpClient not sending the request to Web Api async get method

I haven't used Web Api in a while and I am stuck with something that should be very simple. 我已经有一段时间没有使用Web Api了,但我坚持使用应该很简单的方法。

I have an asynchronous get method in a Web Api controller: 我在Web Api控制器中有一个异步get方法:

public class SomeController : ApiController  
{  
   [HttpGet]  
   public async Task<IHttpActionResult> MyMethodAsync(Guid id)  
   {  
      //... doing something.
   }  
}

On my client application I am trying to call it like so: 在我的客户端应用程序上,我试图这样称呼它:

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);
}

The problem that I have is that it seems that the request is not even being sent to the Web Api since the break point that I am setting on MyMethodAsync has the warning that the symbols are not being loaded. 我遇到的问题是,由于我在MyMethodAsync上设置的断点发出警告,提示您符号未加载,似乎请求甚至没有发送到Web Api。

What really confuses me is that the next call is to a POST method on the same controller that when I call it the symbols load and its breakpoint gets hit. 真正令我困惑的是,下一个调用是在同一控制器上的POST方法,当我调用它时,符号加载并且其断点被命中。

Like I said, I am comming back to Web Api after a really long time and quite a few things are fuzzy in my mind. 就像我说的那样,经过很长时间之后,我又回到了Web Api,很多事情在我的脑海中变得模糊。 I only have the default route so, I do not know if I have to add more specific routes and if so, how to specify them. 我只有默认路由,所以我不知道是否必须添加更多特定的路由,如果要添加,则如何指定它们。

As a side note I have two other GET methods that also have a Guid parameter. 附带说明一下,我还有另外两个具有Guid参数的GET方法。 I have tried calling those with the same result. 我试过给那些结果相同的人打电话。

Thank you for your help. 谢谢您的帮助。

UPDATE : To make matters more confusing. 更新使事情更加混乱。 If I run the application but instead of using the client I open a browser and enter the url exactly as the client would do it, the break point in the action gets hit. 如果我运行该应用程序,但没有使用客户端,而是打开浏览器,并完全按照客户端的方式输入URL,则操作中的断点会被命中。 Right after that I try using the client and then it won't. 在那之后,我尝试使用客户端,然后就没有了。

1. Open App_Start / WebApiConfig.cs 1.打开App_Start / WebApiConfig.cs

在此处输入图片说明

2. Add New Route 2.添加新路线

config.Routes.MapHttpRoute(
    name: "NewRoute",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

2.1. 2.1。 FULL Code 完整代码

在此处输入图片说明

3. USE Action Name Attribute [ActionName("MyMethod1")] 3. USE动作名称属性[ActionName(“ MyMethod1”)]

    [ActionName("MyMethod1")]
    [HttpGet]
    public IHttpActionResult MyMethod1(Guid id)
    {
        return Ok<string>($" ok MyMethod1 and id= {id}");

    }

4. Same for MyMethod2 4.与MyMethod2相同

    [ActionName("MyMethod2")]
    [HttpGet]
    public IHttpActionResult MyMethod2(Guid id)
    {
        return Ok<string>($" ok MyMethod2 and id= {id}");

    }

Extra Note : For the symbols are not being delete error in visual studio 格外注意:对于在Visual Studio中未删除符号的错误

  • close visual studio 关闭视觉工作室
  • Delete WebApplication1\\bin 删除WebApplication1 \\ bin
  • Delete WebApplication1\\obj 删除WebApplication1 \\ obj
  • Open visual studio 开放式视觉工作室
  • build the solution 建立解决方案
    在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM