HTTPPost MVC 4 web api

本文关键字:api web MVC HTTPPost | 更新日期: 2023-09-27 18:27:40

我正在构建一个mvc 4 web api,但当我尝试向web api发布时,请求会返回

"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?

感谢

HTTPPost MVC 4 web api

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