2012-03-14 130 views
3

我建立一个MVC 4个Web API,但是当过我尝试做一个发布到网络API请求返回HTTPPost MVC 4个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); 
} 

的方法我用

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

的URL我仍然新本,但无法弄清楚,为什么不支持POST?

感谢

+0

你如何提出要求? – gdoron 2012-03-14 11:08:58

+0

我在提琴手中构建它,然后单击执行 – Armand 2012-03-14 11:12:42

+0

您的控制器是否称为ServiceController? – 2012-03-14 11:16:55

回答

5

尝试使用http://localhost:53393/api/service为您的URI,因为你目前没有在您的API路线的{}动作段。

+0

添加了{动作}到URI模板,它的工作,也许更新答案反映这 – Armand 2012-03-14 11:24:18

+0

@Armand无论哪种方式是有效的。如果您打算采用更多的面向资源的方法,您会为您指定方法Post()和您的控制器Authenticate,然后您将不需要{action},并且可以放弃'service'路径段。 – 2012-03-14 11:34:11