Web API 2 GET by request parameter

I'm going to switch from my WCF Rest / Json service to WebApi2, and I'm looking for a way to map this method:

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

I want to request the user by email and password, so I can not use the default GET, which is designed to use the identifier. As far as I know, you should do this using attributes in Rest ...

Do I just need to register a route for this, or is there a better way (perhaps by agreement)?

+7
asp.net-web-api asp.net-web-api2
source share
1 answer

You always need to register a route in WebApi for the actions of your controller, this can be done using attribute routing or based on conventions .

The parameters passed in the query string for the GET request need not be explicitly specified in any of the routing configuration methods.

The parameters you specify in the controller action are displayed in the parameters sent in the GET request string.

If you use the default default WebApi-based configuration, where the routes are configured something like this:

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

Then such a controller will work for you:

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

Alternatively, you can use attribute routing, and then call your controllers and action methods no matter what you want. Configure your routes as follows:

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

Then you can create a controller like this:

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

Keep in mind that using attribute routing, you must be careful not to create conflicting routes, otherwise WebApi will not know which controller and action to send the request when the route is mapped to several action methods.

I used this.Json in these examples to return an HTTP response with json content according to your wcf ResponseFormat = WebMessageFormat.Json . But you can, of course, just return the CLR type:

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

and enable content negotiation in WebApi.

+10
source share

All Articles