Robert Pouleijn
Robert Pouleijn

Reputation: 509

404 when passing web api parameters

I'm trying to get this method to work:

public class DocumentenController : ApiController
{    
    [HttpPost]
    [Route("DeleteDocument/{name}/{planId}")]
    public IHttpActionResult DeleteDocument(string name, int planId)
    {
      _documentenProvider.DeleteDocument(planId, name);
      return Ok();
    }
}

This is the WebApiConfig :

config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
    name: "ActionApi",
    routeTemplate: UrlPrefix + "/{controller}/{action}/{id}",
    defaults: new {id = RouteParameter.Optional}
);

But I get a 404 when I call it like this using a post:

http://localhost/QS-documenten/api/documenten/deletedocument/testing/10600349

What is the proper way do solve this?

Upvotes: 4

Views: 1976

Answers (2)

Morteza Zabihi
Morteza Zabihi

Reputation: 3044

You must send your request as follows:

http://localhost/QS-documenten/deletedocument/testing/10600349

When you use route attribute, the custom route override default api routing configuration.

Upvotes: 2

Nkosi
Nkosi

Reputation: 246998

The URL in example does not match attribute route on controller.

To get

http://localhost/QS-documenten/api/documenten/deletedocument/testing/10600349

to work, assuming that http://localhost/QS-documenten is the host and root folder, and that api/documenten is the api prefix then add a route prefix to the controller...

[RoutePrefix("api/Documenten")]
public class DocumentenController : ApiController {
    //eg POST api/documenten/deletedocument/testing/10600349
    [HttpPost]
    [Route("DeleteDocument/{name}/{planId}")]
    public IHttpActionResult DeleteDocument(string name, int planId) {
        _documentenProvider.DeleteDocument(planId, name);
        return Ok();
    }
}

Source: Attribute Routing in ASP.NET Web API 2 : Route Prefixes

Upvotes: 4

Related Questions