Tuximo
Tuximo

Reputation: 57

Issue with a WebApi action controller

my controller action:

public IHttpActionResult GetPdf(int file)

the call ajax:

var self = this;
    var url = "api/pdfs/1";
    $.getJSON(url, function (data)...

The routing pattern:

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

When I execute the code I have a HTTP 404 error. Can someone see the problem? Thank you.

Upvotes: 0

Views: 28

Answers (1)

David
David

Reputation: 218807

The ongoing versions of WebAPI may have varying conventions, but I think the safest approach in simple cases is to name the method after the HTTP verb. Additionally, note that your routing is expecting an id parameter:

routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }

But you're naming your parameter file instead. Combine these two suggestions and re-define your method as:

public IHttpActionResult Get(int id)

Upvotes: 1

Related Questions