Web API 2 GET by query参数

ste*_*fan 11 asp.net asp.net-web-api asp.net-web-api2

我要从我的WCF Rest/Json服务切换到WebApi2,我正在寻找一种方法来映射这个方法:

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "Users?mail={mail}&pw={pw}")]
UserData getUserByEmailAndPw(String mail);
Run Code Online (Sandbox Code Playgroud)

我想通过电子邮件和密码查询用户,因此我无法使用打算使用ID的默认GET.据我所知,你应该通过Rest中的属性来做到这一点......

我是否只需要为此注册路线或者是否有更好的方法(可能按照惯例)?

Ani*_*tel 16

您始终必须在WebApi中为控制器操作注册路由,这可以通过属性路由基于约定的路由来完成.

在GET请求的查询字符串中传递的参数实际上不必在任何一个路由配置方法中明确指定.

您在控制器操作上指定的参数将映射到在GET请求的查询字符串中发送的参数.

如果您使用默认的基于WebApi约定的设置,其中路由配置如下:

var config = new HttpConfiguration();
// some other config setup for web api
...
...
// route config
config.Routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)

那么像这样的控制器将适合你:

public class UsersController : ApiController {
   // this maps to a get requests to:
   // domain/api/users
   // and domain/api/users?id=someid
   // and domain/api/users?mail=somemail
   // and domain/api/users?pw=somepw
   // and domain/api/users?mail=somemail&pw=somepw
   // and domain/api/users with any query string really
   [HttpGet]
   public IHttpActionResult Get(string mail, string pw) {
      // should probably check mail and pw for empty strings and nulls
      var users = SomeStaticExampleService.FindByMailAndPw(mail, pw);
      return this.Json(users);
   }
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用属性路由,然后根据需要调用控制器和操作方法.像这样配置你的路线:

var config = new HttpConfiguration();
// some other config setup for web api
...
...
// route config
config.MapHttpAttributeRoutes();
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样创建一个控制器:

public class FooController : ApiController {
   // this maps to a get requests to:
   // domain/users
   // and domain/users?id=someid
   // and domain/users?mail=somemail
   // and domain/users?pw=somepw
   // and domain/users with any query string really
   [HttpGet]
   [Route("users")]
   public IHttpActionResult Bar(string mail, string pw) {
      // should probably check mail and pw for empty strings and nulls
      var users = SomeStaticExampleService.FindByMailAndPw(mail, pw);
      return this.Json(users);
   }
}
Run Code Online (Sandbox Code Playgroud)

请记住,尽管使用属性路由,您必须小心不要创建冲突路由,否则WebApi将不知道将路由映射到多个操作方法时将请求路由到哪个控制器和操作.

this.Json在这些示例中使用了返回带有json内容的http响应以匹配您的wcf ResponseFormat = WebMessageFormat.Json.但是你当然可以返回一个CLR类型:

   [HttpGet]
   [Route("users")]
   public IEnumerable<MyUser> Bar(string mail, string pw) {
      // should probably check mail and pw for empty strings and nulls
      var users = SomeStaticExampleService.FindByMailAndPw(mail, pw);
      return users;
   }
Run Code Online (Sandbox Code Playgroud)

WebApi的 内容协商处理响应消息内容类型.

  • @ user1944491您可以在参数上使用`[FromUri]`属性,例如`public IEnumerable <MyUser> Bar([FromUri] string id,string open,string closed)`,你的路由定义如下`[Route("users) /(编号)")]` (8认同)
  • 如果您想将 URL 参数与查询参数结合起来,例如 /users/4333?open=yes&amp;angled=maybe ,该怎么办? (2认同)