【问题标题】:Attribute routing not working属性路由不起作用
【发布时间】:2013-07-02 12:58:52
【问题描述】:

我是使用 aspnet web api 进行属性路由的新手。

public class UsersController : ApiController
{
    [GET("users/me/brands/{id}")]
    public IEnumerable<Brand> GetBrands(long id)
    {
        return _repository.FindByUser(id);
    }
}

但我无法执行此操作。我尝试了很多方法:

  1. http://example.com/api/users/brands/4
  2. http://example.com/api/users/brands?id=4
  3. http://example.com/users/brands?id=4
  4. http://example.com/users/brands/4

PS:我也映射为[GET("api/users/me/brands/{id}")]

我错过了什么?

【问题讨论】:

  • 您的属性路由显示users/me/brands/{id},而您使用的是api/users/brands/4 等。为什么?如果您希望执行此操作,则应该遵循模板的建议...即users/me/brands/4...
  • @KiranChalla api 是默认前缀。没有它,mvc 不会路由到我的控制器

标签: c# asp.net-web-api wcf-web-api


【解决方案1】:

在使用 MVC 4 和 4.5 的 Web api 路由中存在一个错误

可以找到完整的解释和解决方法

MVC 4.5 Web API Routing not working?

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 2014-12-27
    • 2016-01-06
    • 2018-04-30
    • 2014-03-25
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多