Marcos Foster
Marcos Foster

Reputation: 67

ASP.NET Web API - GET HTTP Verb not supported (405) when attempting to pass a parameter

I am attempting to call my Web API with a getJSON request:

var uri = 'api/comment';
var id = solicitorId;

$.getJSON(uri, id, (function (data) {
    $('#commentTableContainer').html(data);
}));

And this is the method in the comment Controller class:

public string GetComment(int id)
{
    //Do things
}

I am using the default routing:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { action = "Index", id = UrlParameter.Optional }
            );

However, when trying to call the api with getJSON I receive a 405 error:

HTTP405: BAD METHOD - The HTTP verb used is not supported.
(XHR)GET - http://localhost:<port>/api/comment?334203

The GET request works if I remove the id parameter from GetComment signature, i.e. GetComment()

I don't know too much about this WebAPI stuff - I mostly followed a guide from Microsoft, here here (learn.microsoft.com)

If anyone has any ideas I would be grateful. I've looked at many SO questions on this, but none have helped.

I've tried adding [HTTPGet] to the CommentController GetComment(int id) method, as well as specifying the route with [Route] but I am getting nowhere at all.

Any help would be much appreciated. Thanks.

Upvotes: 2

Views: 2978

Answers (4)

first of all change WebApiConfig file and edit default route like below .

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

When you change route template and add "{action}" parameter , this mean you must add action name when you want to call action

and after that you can call function like below url

var uri = 'http://localhost:<port>/api/comment/GetComment/'+id;

I hope this help you

Upvotes: 0

Marcos Foster
Marcos Foster

Reputation: 67

and thank you so much for the help. I have fixed it now. I don't really know exactly how to describe this, but I changed my $.getJSON call to this:

var uri = 'api/comment/' + id;
$.getJSON(uri, (function (data) {
    //immaterial things
}

And my CommentController method back to just public string GetComment(int id) without the [Route] or [HTTPGet]

And it now works. I completely thought I had tried this before, but no. I would not have been able to fix this without everyone's suggestions so thank you so much and have a good weekend!

Upvotes: 0

D-Shih
D-Shih

Reputation: 46219

Your Route config might not match your URL.

Route config : url: "{controller}/{action}/{id}"

Request URL : /api/comment?334203

you can try to add Route attribute to set RouteAttribute for your API action.

[Route("api/comment/{id}")]
[HttpGet]
public string GetComment(int id)
{
    //Do things
}

and you need to use full url on your ajax request.

var uri = 'http://localhost:<port>/api/comment/GetComment';

that can match your route setting.

Upvotes: 4

Shyam Sa
Shyam Sa

Reputation: 331

Just try the url in the browser =http://localhost:/api/comment/GetComment?334203. Maybe you are missing the method name in from the URL.

Also, the webApi.Config is used for the Web API. Let me know if it not works.

Upvotes: 1

Related Questions