C# 将Id和字符串传递给WebAPI Get
我正在使用新的MVC Web Api,我有如下内容:C# 将Id和字符串传递给WebAPI Get,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,我正在使用新的MVC Web Api,我有如下内容: public class CustomersController : ApiController { public HttpResponseMessage Get() { //Return Somethings } } 我用/api/customers来称呼它 现在我希望能够根据国籍和身份进行筛选,所以我做了如下: public class Customer
public class CustomersController : ApiController
{
public HttpResponseMessage Get()
{
//Return Somethings
}
}
我用/api/customers来称呼它
现在我希望能够根据国籍和身份进行筛选,所以我做了如下:
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
?类似的方法应该可以做到这一点
routes.MapHttpRoute(
name: "CustomersByIdAndNat",
routeTemplate: "api/customers/{Id}/{Nat}/",
defaults: new { controller = "Customers" }
);
作为旁注,/1/us
不是查询字符串。是URL上的?
符号之后的内容。例如,在你的情况下,可能是这样的
api/customers/?id=1&nat=us
不是说你需要改变url格式,只是解释一下这个概念。类似的东西应该可以解决这个问题
routes.MapHttpRoute(
name: "CustomersByIdAndNat",
routeTemplate: "api/customers/{Id}/{Nat}/",
defaults: new { controller = "Customers" }
);
作为旁注,/1/us
不是查询字符串。是URL上的?
符号之后的内容。例如,在你的情况下,可能是这样的
api/customers/?id=1&nat=us
没有说您需要更改url格式,只是解释了这个概念。/api/customers/1/us是一个嵌入值,而不是查询字符串。不幸的是,我不知道为什么它不调用重载Get。检查路由可能?/api/customers/1/us是嵌入值,而不是查询字符串。不幸的是,我不知道为什么它不调用重载Get。也许查一下路线?