【问题标题】:Pass Id and String to WebAPI Get将 Id 和 String 传递给 WebAPI Get
【发布时间】:2012-09-22 05:02:29
【问题描述】:

我正在使用新的 MVC Web Api 我有这样的东西:

    public class CustomersController : ApiController
    {
  public HttpResponseMessage Get()
        {
            //Return Somethings
        }
    }

我用 /api/customers 调用它

现在我希望能够按国籍和 id 进行过滤,所以我做了这个:

public class CustomersController : ApiController
{
    public HttpResponseMessage Get(int Id, String Nat)
    {
        //and i filter here Return Somethings
    }
}

如果我尝试使用/api/customers/1/us 它调用第一个方法,我知道这些是查询字符串,那么我如何定义路由以便我可以使用/api/customers/1/us

【问题讨论】:

  • /api/customers/1/us 是嵌入值,而不是查询字符串。不幸的是,我不确定它为什么不调用重载的 Get。看看路线吧?

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


【解决方案1】:

这样的事情应该可以解决问题

routes.MapHttpRoute(
     name: "CustomersByIdAndNat",
     routeTemplate: "api/customers/{Id}/{Nat}/",
     defaults: new { controller = "Customers" }
);

附带说明,/1/us 不是查询字符串。 Querystring 是在网址上的 ? 符号之后出现的。例如在你的情况下,可能是这样的

api/customers/?id=1&nat=us

不是说你需要改变url格式,只是解释一下概念。

【讨论】:

    猜你喜欢
    • 2017-06-18
    • 2018-05-01
    • 1970-01-01
    • 2016-04-01
    • 2012-12-18
    • 2015-11-20
    • 2015-07-02
    • 2015-09-26
    • 2015-06-07
    相关资源
    最近更新 更多