gpt4 book ai didi

c# - 路由不触发 Asp.Net Core

转载 作者:行者123 更新时间:2023-11-30 16:46:28 26 4
gpt4 key购买 nike

我不确定我的路线有什么问题。当我尝试访问路由时,断点没有触发。

这是来自 googles advanced rest client 的源消息

POST /api/menu HTTP/1.1
HOST: localhost:6223
content-type: application/json
content-length: 34

{ "app_id": 99999, "user_type": 2}

这是我的 Controller

[Route("api/[controller]")]
public class MenuController : Controller
{
private MenuRepo xMenuRepo;
public MenuController(IOptions<SqlConnectionStringsList> iopt)
{
xMenuRepo = new MenuRepo(iopt);
}

[HttpPost]
public IEnumerable<Menu> GetMenuItems([FromBody] MenuQuery menuq)
{
List<Menu> xMenuList = new List<Menu>();
xMenuList = xMenuRepo.GetMenuItems(menuq.app_id, menuq.user_type);
return xMenuList;
}
}

public class MenuQuery
{
public int app_id { get; set; }
public int user_type { get; set; }
}

我在 GetMenuItems 上放置了一个断点,它甚至没有触发。响应是 500 内部服务器错误。这个项目只有两条路由,新建项目默认的ValuesController,还有这个MenuController。除了在 Controller 的顶部,我找不到任何额外的地方来指定项目中的路由。但是 ValuesController 有效!我不知道哪里出了问题!?

最佳答案

缺少行动路线

Routing to Controller Actions: Attribute Routing

Mixed Routing

MVC applications can mix the use of conventional routing and attribute routing. It’s typical to use conventional routes for controllers serving HTML pages for browsers, and attribute routing for controllers serving REST APIs.

Actions are either conventionally routed or attribute routed. Placing a route on the controller or the action makes it attribute routed. Actions that define attribute routes cannot be reached through the conventional routes and vice-versa. Any route attribute on the controller makes all actions in the controller attribute routed.

[Route("api/[controller]")]
public class MenuController : Controller
{
private MenuRepo xMenuRepo;
public MenuController(IOptions<SqlConnectionStringsList> iopt)
{
xMenuRepo = new MenuRepo(iopt);
}

//POST api/menu
[HttpPost("")]
public IEnumerable<Menu> GetMenuItems([FromBody] MenuQuery menuq)
{
List<Menu> xMenuList = new List<Menu>();
xMenuList = xMenuRepo.GetMenuItems(menuq.app_id, menuq.user_type);
return xMenuList;
}
}

关于c# - 路由不触发 Asp.Net Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40369092/

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