0

I know you can alter the default behaviour of routing in Web API like this:

routes.MapHttpRoute(
    name: "ActionApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

But can you

routes.MapHttpRoute(
    name: "ActionApi",
    routeTemplate: "api/{controller}?verb={action}",
    defaults: new { action = RouteParameter.Optional }
);

Edit: The following route templates gives the following errors:

"api/{controller}?verb={action}"

throws a YSOD becaus of the ?-character

"api/{controller}%3Fverb={action}"

Throws the following

Multiple actions were found that match the request: GetRecord on type RouteConfigMedGet.Controllers.TestController ListRecords on type RouteConfigMedGet.Controllers.TestController

Fontanka16
  • 1,161
  • 1
  • 9
  • 37

1 Answers1

1

As I know you cannot write routes with query string params.

If you want to make sure that your controller's action will handle requests only with specific query params you may create custom route constraint where you will check if required param is present and add it as constraint to your route.

In general solution to use query params to devide different actions is realy strange. I copied nice explanation from this thread:

Routes are meant to delineate one resource from another, whereas as you stated, QueryString parameters are meant for Presentation logic*. Therefore you cannot expect a URL pointing to a resource to go to a different resource if the same URL is used, but with querystring parameters. This is not a failing of MVC, rather I don't believe you're using Querystrings correctly.

by George Stocker.

Community
  • 1
  • 1
ntl
  • 1,289
  • 1
  • 10
  • 16
  • Thanks for your answer, however i would like some kind of reference. I can only find imlicit statements in documentation that suggest that it is not possible. – Fontanka16 Aug 21 '14 at 05:11
  • I very much agree with George Stocker and i only whish that the guys who put forward the specs for the protocol i am implementing had done the same... – Fontanka16 Aug 21 '14 at 05:12