C# HTTPPost MVC 4 web api

C# HTTPPost MVC 4 web api,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,我正在构建一个MVC4WebAPI,但每当我尝试向WebAPI发送帖子时,请求都会返回 "The requested resource does not support http method 'POST'." 我的请求头 User-Agent: Fiddler Host: localhost:53393 Content-Length: 39 Content-Type: application/json 我的请求机构 { "username":"", "password":"" }

我正在构建一个MVC4WebAPI,但每当我尝试向WebAPI发送帖子时,请求都会返回

"The requested resource does not support http method 'POST'."
我的请求头

User-Agent: Fiddler
Host: localhost:53393
Content-Length: 39
Content-Type: application/json
我的请求机构

{
  "username":"",
  "password":""
}
路线

    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 }
        );
    }
以及我的控制器中的方法

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

http://localhost:53393/api/service/authenticate
我对此还是新手,但不明白为什么不支持POST


谢谢

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

如何发出post请求?我在fiddler中构建它,然后单击Execute。您的控制器名为ServiceController吗?是的,正常的GET请求工作正常添加了{action}对于URI模板,可能会更新答案以反映this@Armand任何一种方法都是有效的。如果您打算采用更面向资源的方法,您可以将方法命名为Post(),控制器进行身份验证,这样您就不需要{action},您可以删除“服务”路径段。