【问题标题】:Web API 2 GET by query parameterWeb API 2 通过查询参数获取
【发布时间】:2015-10-23 22:45:01
【问题描述】:

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

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

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

我是否只需要为此注册一条路线还是有更好的方法(也许按照惯例)?

【问题讨论】:

  • 我知道这是旧的,但对于这个问题的新观众:在查询字符串中传递密码(或任何敏感数据)通常被认为是不好的做法。见stackoverflow.com/a/323286/2391294
  • 同意!但这只是一个示例,问题是“如何使用查询参数创建 GET”...
  • 酷,完全明白了。只是想为做“从 StackOverflow 复制和粘贴”事情的新手指出这一点。 ??????

标签: asp.net asp.net-web-api asp.net-web-api2


【解决方案1】:

您总是必须在 WebApi 中为您的控制器操作注册一个路由,这可以使用 attribute routingconventions based routing 来完成。

在 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 }
);

那么像这样的控制器会为你工作:

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);
   }
}

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

var config = new HttpConfiguration();
// some other config setup for web api
...
...
// route config
config.MapHttpAttributeRoutes();

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

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);
   }
}

请记住,尽管使用 Attribute Routing 您必须小心不要创建冲突路由,否则 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;
   }

并让 WebApi 的 content negotiation 处理响应消息内容类型。

【讨论】:

  • 如果您想将 URL 参数与查询参数结合起来,例如 /users/4333?open=yes&closed=maybe 怎么办?
  • @user1944491 您可以在参数上使用[FromUri] 属性,例如public IEnumerable&lt;MyUser&gt; Bar([FromUri]string id, string open, string closed),您的路由定义如下[Route("users/{id}")]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 2018-03-27
相关资源
最近更新 更多