Web Api 1 | .NET 4.5.2. | routing with http verbs instead of actions, without external libraries

86 Views Asked by At

I'm not finding this info anywhere on MSDN, SO, other sites, and i've been searching for days. I'm on a WEB API 1 - .NET 4.5.2 project, and i need to expose a CRUD controller. This means I can't use some powerful attributes in WebApi2, like:

  • [Route("...")]
  • [RoutePrefix("...")]
  • ...

I need my api to use this form:

prot://url:port/api/mycontroller/{tablename}/{id}

where {tablename} is already a parameter to be used, and id si optional.

Using Postman, let's say, I want:

  • the Action Get(string tablename) to be invoked when I execute a GET | prot://url:port/api/mycontroller/{tablename}
  • the Action Post(string tablename) to be invoked when I execute a POST | prot://url:port/api/mycontroller/{tablename}
  • and so on and I want this behavior exclusively for this specific controller.
public class SpecificController : ApiController { 
    [HttpGet]
    public object Get(string tablename, string id) { . . . }
    [HttpPost]
    public object Post(string tablename) { . . . }

    . . .
 }

I added a new routing on my WebApiConfig.Register class method:

config.Routes.MapHttpRoute(
  name: "SpecificApi",
  routeTemplate: "api/{controller}/{table}/{id}",
  constraints: new { controller = "/^specific$" },
  defaults: new { id = RouteParameter.Optional }
);

I build, launch, and it seems fine. When executing calls via Postman, though, i receive the following error message

any verb | http://localhost:port/api/specific/mytable

"Message":"No HTTP resource was found that matches the request URI 'http://localhost:port/api/specific/mytable'."
"MessageDetail": "No action was found on the controller 'Specific' that matches the name 'mytable'."

It works fine if I use the Default routing, meaning adding the Action (Get(), Post(), ...) after the name of the controller...

I'm not getting this, why is it only matching Actions and not Verbs? Also, I'm not supposed to use other third parties libraries, apart those provided by Microsoft.

Thanks in advance

0

There are 0 best solutions below