gpt4 book ai didi

asp.net-core-mvc - 基于 HTTP 动词的路由操作?

转载 作者:行者123 更新时间:2023-12-01 16:10:20 29 4
gpt4 key购买 nike

我试图让 ASP.NET Core 2 MVC 通过 Startup.cs 中的以下代码根据 HTTP 动词路由操作:

        app.UseMvc(routes =>
{
routes.MapRoute(
name: "post",
template: "api/{controller}/{id?}",
defaults: new { action = "Post" },
constraints: new RouteValueDictionary(new { httpMethod = new HttpMethodRouteConstraint("POST") })
);
routes.MapRoute(
name: "delete",
template: "api/{controller}/{id?}",
defaults: new { action = "Delete" },
constraints: new RouteValueDictionary(new { httpMethod = new HttpMethodRouteConstraint("DELETE") })
);
routes.MapRoute(
name: "default",
template: "api/{controller}/{action=Get}/{id?}");
});

即,

  • 如果客户调用 GET http://example.com/api/foo ,运行 Get()我的方法FooController : Controller类。
  • 如果他们打电话 GET http://example.com/api/foo/123 ,运行 Get(int id)我的方法FooController : Controller类。
  • 如果他们打电话 POST http://example.com/api/foo ,运行 Post([FromBody] T postedItem)我的方法FooController<T> : Controller类。
  • 如果他们打电话 POST http://example.com/api/foo/123 ,运行 Post(int id, [FromBody] T postedItem)我的方法FooController<T> : Controller类。
  • 如果他们打电话 DELETE http://example.com/api/foo/123 ,运行 Delete(int id)我的方法FooController : Controller

当我运行该项目时,它似乎没有运行我的任何 Controller 。我有一些 Razor 页面可以响应,但所有基于 Controller 的路由都只返回 404。甚至默认路由似乎也不起作用。

我一直在使用https://github.com/ardalis/AspNetCoreRouteDebugger尝试帮助我缩小问题范围,但我仍然没有找到问题。它将 Controller 上的方法显示为可用操作,但未列出通过 MapRoute 添加的任何名称、模板或约束。 。我也很高兴了解任何其他有用的工具。

FWIW,我尝试使用与此处相同的动词约束: https://github.com/aspnet/Routing/blob/2.0.1/src/Microsoft.AspNetCore.Routing/RequestDelegateRouteBuilderExtensions.cs#L252-L268

最佳答案

所以我不记得问题到底是什么,但元解决方案是您可以通过将日志级别从“信息”增加到“调试”来调试路由问题。例如,通过 appsettings.json:

{
"Logging": {
"Debug": {
"LogLevel": {
"Default": "Debug"
}
},
"Console": {
"LogLevel": {
"Default": "Debug"
}
}
}
}

...然后您将在 Visual Studio 的应用程序输出 Pane 中收到类似这样的消息:

[40m[37mdbug[39m[22m[49m: Microsoft.AspNetCore.Routing.RouteConstraintMatcher[1]
Route value '(null)' with key 'httpMethod' did not match the constraint 'Microsoft.AspNetCore.Routing.Constraints.HttpMethodRouteConstraint'.
Microsoft.AspNetCore.Routing.RouteConstraintMatcher:Debug: Route value '(null)' with key 'httpMethod' did not match the constraint 'Microsoft.AspNetCore.Routing.Constraints.HttpMethodRouteConstraint'.
[40m[37mdbug[39m[22m[49m: Microsoft.AspNetCore.Routing.RouteBase[1]
Request successfully matched the route with name 'get' and template 'api/{controller}/{id?}'.
Microsoft.AspNetCore.Routing.RouteBase:Debug: Request successfully matched the route with name 'get' and template 'api/{controller}/{id?}'.
[40m[37mdbug[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Executing action Contoso.Media.ServiceHost.Controllers.MediaController.Get (Contoso.Media.ServiceHost)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Debug: Executing action Contoso.Media.ServiceHost.Controllers.MediaController.Get (Contoso.Media.ServiceHost)

关于asp.net-core-mvc - 基于 HTTP 动词的路由操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47995696/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com