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

标签 c# asp.net asp.net-web-api routes asp.net-core

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

这是来自 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/

相关文章:

c# - 当放置在 FormView 控件中时设置 asp .net Rating 控件值

asp.net-web-api - 查询后修改odata结果

c# - OWIN ASP.NET - 避免在 Web Api 中没有身份的同一帐户多次登录

c# - 如何在代码隐藏中的转发器内部的 div 上设置类?

c# - WebApi 路由返回 Not Found in Orchard Module

c# - FindAsync 和 Include LINQ 语句

c# - 请向我解释一下这个 SerializationException

c# - 如何从数组中获取项目并将它们显示在文本框中;

c# - 如何在 Silverlight 中查找可见的 DataGrid 行?

c# - JavaScript 与 C# 不起作用