2012-03-14 127 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