3

I am building a mvc 4 web api but when ever I try to do a post to the web api the request returns

"The requested resource does not support http method 'POST'."

My Request Headers

User-Agent: Fiddler
Host: localhost:53393
Content-Length: 39
Content-Type: application/json

My Request Body

{
  "username":"",
  "password":""
}

Routes

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        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 }
        );
    }

And the method in my controller

[HttpPost]
public MethodResponse Authenticate(string username, string password)
{
    ConsoleServiceClient service = new ConsoleServiceClient();
    return service.Authenticate(username, password);
}

The URL I use

http://localhost:53393/api/service/authenticate

I am still new to this, but can't figure out why POST is not supported?

Thanks

4
  • How do you make the post request?
    – gdoron
    Mar 14, 2012 at 11:08
  • I build it in fiddler and click execute
    – Armand
    Mar 14, 2012 at 11:12
  • Is your controller called ServiceController? Mar 14, 2012 at 11:16
  • yes, normal GET requests work fine
    – Armand
    Mar 14, 2012 at 11:19

1 Answer 1

5

Try using http://localhost:53393/api/service as your URI because you currently don't have an {action} segment in your API route.

2
  • Added the {action} to the URI template and it worked, maybe update answer to reflect this
    – Armand
    Mar 14, 2012 at 11:24
  • @Armand Either way is valid. If you are going for a more Resource Oriented approach you would name you method Post() and your controller Authenticate and then you wouldn't need the {action} and you could drop the 'service' path segment. Mar 14, 2012 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.