0

I want to do a simple REST API for a functionality my webservice will expose.

[RoutePrefix("companies")]
public class CompaniesController : BaseApiController {

    [HttpGet, Route("{companyId:int}")]
    public CustomResponse Get(int companyId) { }

    [HttpPost]
    public CustomResponse Post(CompanySaveViewModel model) { }

    [HttpDelete, Route("{companyId:int}"]
    public CustomResponse Delete(int companyId) { }
}

Ok, this should be working. POST method is working fine. However, when I try to call GET and/or DELETE methods, I got the message below:

I'm trying to call those methods using the given URLs:

http://localhost:11111/api/companies/1 [GET]

http://localhost:11111/api/companies/1 [DELETE]

enter image description here

POST is working fine. When I try to call GET without parameters, it works fine as well. The problem appears when I have any kind of parameter for GET/DELETE methods. What could be the problem here?

Thank you all for the help!

1
  • remove the RoutePrefix and try the same Commented Jan 7, 2017 at 8:21

1 Answer 1

2

Try adding a route to your POST action:

[HttpPost, Route("")]
public CustomResponse Post(CompanySaveViewModel model) { }

This will ensure consistency in your routing definitions: either you use attribute based routing, or you use the global convention based routing (personally I prefer explicit attribute based routing). I would also recommend you to avoid mixing the two types of routes and remove the convention based route from your config:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

Also don't forget to enable attribute based routing when bootstrapping:

config.MapHttpAttributeRoutes();

Here's a nice overview of attribute based routing that I would recommend you going through.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, @darin! By removing the default HTTP routing it started to work like a charm. I think the application was missleading the routing convention and didn't know if it'll need to use Route convention or base-defined convention. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.