简体   繁体   中英

HTTPPost MVC 4 web api

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

尝试使用http://localhost:53393/api/service作为您的URI,因为您当前在API路由中没有{action}段。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM