【问题标题】:Access to controller method访问控制器方法
【发布时间】:2014-08-21 11:24:40
【问题描述】:

我有一些方法 GET 的控制器:

public class TestController : ApiController
{
   public List<T> Get(){...}

   [ActionName("GetById")]
   public T Get(int id){...}
}

即使我有不同的 ActionName,我能否以 /Get?id=1 访问第二个 Get 方法?

ActionName 用于生成不同名称的缓存

【问题讨论】:

  • 你为什么不试试看呢?
  • @RoyDictus,我尝试过但没有成功,所以我认为 WebApiConfig 中的一些更改可能会有所帮助

标签: c# asp.net-mvc action


【解决方案1】:

更新是因为我之前的回答与标准 MVC 控制器而不是 Web API 有关,因为这就是 ActionName 属性的用途。我不确定它会在 web api 控制器上做什么。如果没有属性或更改默认路由,您的操作将具有以下路由 "/api/test/" Get() "/api/test/id" Get(int id) 其中 id 是一个 int。

如果你想要更多的灵活性,MVC5 支持attribute routing

【讨论】:

    猜你喜欢
    • 2014-01-11
    • 2016-03-18
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2022-12-03
    • 2015-08-02
    相关资源
    最近更新 更多