【问题标题】:No candidates found for the request even though the route exists即使路由存在,也没有找到该请求的候选人
【发布时间】:2021-08-19 11:09:14
【问题描述】:

我有一个具有以下声明的控制器

[Authorize(Roles = Role.Admin)]
[ApiController]
[Route("meta/[controller]")]
public class ActionParameterController : BaseController<ActionParameterController>

里面有如下方法

[HttpPost("insert/{action}/{entity}")]
public IActionResult InsertActionParameter(
    [FromBody] MetaActionParameter parameter,
    int action,
    int entity)

但是,当我尝试向此端点发出 POST 请求时,我得到 404。 网址:

http://localhost:5000/meta/actionParameter/insert/2/2

控制台输出是:

2021-08-19 14:03:35.311 (Microsoft.AspNetCore.Hosting.Diagnostics.POST) [Information] Request starting HTTP/1.1 POST http://192.168.14.104:5000/meta/actionParameter/insert/1/2 - 0
2021-08-19 14:03:38.116 (Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware.) [Verbose] All hosts are allowed.
2021-08-19 14:03:38.117 (Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.POST) [Debug] "POST" requests are not supported
2021-08-19 14:03:38.120 (Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.POST) [Debug] "POST" requests are not supported
2021-08-19 14:03:38.125 (Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.POST) [Debug] "POST" requests are not supported
2021-08-19 14:03:38.132 (Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.) [Debug] AuthenticationScheme: "Bearer" was not authenticated.
2021-08-19 14:03:38.180 (Microsoft.AspNetCore.Routing.Matching.DfaMatcher.) [Debug] No candidates found for the request path '"/meta/actionParameter/insert/1/2"'
2021-08-19 14:03:38.182 (Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.) [Debug] Request did not match any endpoints
2021-08-19 14:03:38.185 (Microsoft.AspNetCore.Server.Kestrel.) [Debug] Connection id ""0HMB303OBKKOL"" completed keep alive response.
2021-08-19 14:03:38.191 (Microsoft.AspNetCore.Hosting.Diagnostics.POST) [Information] Request finished HTTP/1.1 POST http://192.168.14.104:5000/meta/actionParameter/insert/1/2 - 0 - 404 0 - 2880.056

我尝试从IActionDescriptorCollectionProvider 获取所有路线并打印路线。该控制器中还有其他可以工作的路由。

如果我将路由更改为仅“插入”,请求就会通过。据我所知,路线之间没有冲突。

我应该采取哪些进一步的步骤来诊断这个问题?

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    我想我知道问题出在哪里了。

    我认为操作和/或插入字可能是保留字。

    我使用以下代码进行了测试,并且成功了:

        [HttpGet("insert/{xaction}/{xentity}")]
        public IActionResult InsertActionParameterGet(int xaction, int xentity)
        {
            Console.WriteLine(xaction + " " + xentity);
        
            return new JsonResult("Ok");
    }
    

    请注意,我刚刚在动作和实体中添加了一个“x”,作为测试。
    另外,我对 GET 操作进行了测试。
    在添加“x”之前,它不起作用,所以我认为这些可能是保留字。

    【讨论】:

      【解决方案2】:

      你需要在Http Method Attribute处写完整的router,像这样:

      [HttpPost("meta/ActionParameter/insert/{action}/{entity}")]
      public IActionResult InsertActionParameter([FromBody] MetaActionParameter parameter, int action, int entity)
      

      【讨论】:

      • 如果我设置整个路由,实际生成的路由变成meta/ActionParameter/meta/ActionParameter/insert/{action}/{entity},因为控制器有[Route("meta/[controller]")]
      • 你是编译测试,还是猜想?
      • 是的,我测试它只是为了确定。它仍然不起作用。其他路由在这个控制器上工作,只是这个是叛逆的。
      【解决方案3】:

      这里有一个类似的问题:C# Web Api 2 PUT and POST requests "not supported"

      根据那个,您似乎不应该使用 [FromBody]
      所以你的方法签名应该是这样的:

         [HttpPost("insert/{action}/{entity}")]
         public IActionResult InsertActionParameter(int action, int entity)
      

      要准备好请求的正文,您可以执行以下操作:

                  var bodyStr = "";
                  var req = context.HttpContext.Request;
          
                  // Allows using several time the stream in ASP.Net Core
                  req.EnableRewind(); 
          
                  // Arguments: Stream, Encoding, detect encoding, buffer size 
                  // AND, the most important: keep stream opened
                  using (StreamReader reader 
                            = new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
                  {
                      bodyStr = reader.ReadToEnd();
                  }
          
                  // Rewind, so the core is not lost when it looks the body for the request
                  req.Body.Position = 0;
          
                  // Do whatever work with bodyStr here
      

      【讨论】:

      • 我根据你的回答改了签名,但错误依旧存在。
      • 你能分享你的源代码或链接到 git repo,也许可以帮助更多。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多