Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map query parameters with dashes in their names

Is there a way to map the query string parameter my-param to the controller method parameter myParam in Web API 2 (preferably using attribute routing)?

This means a URI like...

library.com/books?search-text=REST

...should be routed to the controller method

[HttpGet, Route("books/{search-text?}")]
public IEnumerable<Book> Get(string searchText = "") { ... }

Is this possible? The Microsoft documentation does not provide an example for that case. But it also doesn't provide some kind of grammar for route parameters, hence I'm not sure if it's exhaustive.

like image 613
Good Night Nerd Pride Avatar asked Jan 04 '16 13:01

Good Night Nerd Pride


1 Answers

You can use the [FromUri] attribute as follows:

[FromUri(Name = "search-text")]

You weren't far off with your comment. If you need this as a convention you can likely create your own parameter binding in Web API:

http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

like image 174
Hux Avatar answered Sep 28 '22 00:09

Hux