16

Within my API Controller called Payment, I have the following method:

[HttpPost]
public HttpResponseMessage Charge(Payment payment)
{
    var processedPayment = _paymentProcessor.Charge(payment);
    var response = Request.CreateResponse(processedPayment.Status != "PAID" ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK, processedPayment);
    return response;
}

In my HTML page I have:

$.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "http://localhost:65396/api/payment/charge",
        data: $('#addPayment').serialize(),
        dataType: "json",
        success: function (data) {
            alert(data);
        }
    });

Whenever I fire the POST, I get

"NetworkError: 405 Method Not Allowed - http://localhost:65396/api/payment/charge"

What am I missing?

Thank you.

UPDATE

Here's the routing information (default)

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
Pavel Jounda
  • 219
  • 4
  • 14
Mike
  • 2,441
  • 7
  • 33
  • 54

3 Answers3

11

Most likely your routing is not configured for the action to be invoked. Hence the request ends up in nowhere and ASP.NET Web API sends a blank-out message "method not allowed".

Can you please update the question with your routing?


UPDATE

As I thought! You are sending to http://localhost:65396/api/payment/charge while you need to send to http://localhost:65396/api/payment - assuming your controller is called PaymentController.

Note that route does not have action.

Aliostad
  • 76,981
  • 19
  • 152
  • 203
  • I'm just using the default routes. I've updated the question. – Mike Jun 06 '12 at 15:39
  • How do I make it invoke the Charge method then? I'm going to have other POST methods within this API controller. – Mike Jun 06 '12 at 15:49
  • With RC you might not be able to. In beta you could add `action` to the route but I heard on twitter (not tried myself) that it cannot be done now. Give it a try with `action` in the route see if it works. – Aliostad Jun 06 '12 at 15:54
  • Turns out I needed to implement CORS support. I used this link as a guide. http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/20/implementing-cors-support-in-asp-net-web-apis.aspx – Mike Jun 11 '12 at 13:09
11

Turns out I needed to implement CORS support. http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/20/implementing-cors-support-in-asp-net-web-apis.aspx

Mike
  • 2,441
  • 7
  • 33
  • 54
  • 5
    This article seems to be more up to date... http://www.stevefenton.co.uk/Content/Blog/Date/201211/Blog/Using-CORS-With-ASP-NET-Web-API/ – Justin Nov 21 '12 at 02:18
1

I had the same problem with my controller. The only thing which is different is the ending of the URL. Add "/" to "http://localhost:65396/api/payment/charge" at the end, that helped me

Pavel Jounda
  • 219
  • 4
  • 14