我正在构建一个 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 }
);
}
还有我 Controller 中的方法
[HttpPost]
public MethodResponse Authenticate(string username, string password)
{
ConsoleServiceClient service = new ConsoleServiceClient();
return service.Authenticate(username, password);
}
我使用的网址
http://localhost:53393/api/service/authenticate
我对此还是个新手,但不明白为什么不支持 POST?
谢谢
最佳答案
尝试使用 http://localhost:53393/api/service
作为您的 URI,因为目前您的 API 路由中没有 {action} 段。
关于c# - HTTPPost MVC 4 网络 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9700706/