404 when passing web api parameters

I am 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 WebApiConfig:

config.MapHttpAttributeRoutes();

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

But I get 404 when I call it this using the message:

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

What is the correct way to solve this problem?

+4
source share
2 answers

The URL in the example does not match the attribute path on the controller.

To obtain

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

to work, considering that it http://localhost/QS-documentenis the main and root folder, and api/documentenis the api prefix, and then adds the 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

+4
source

:

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

, api .

+2

Source: https://habr.com/ru/post/1658875/


All Articles