1
我有以下的AJAX調用:AJAX調用找不到我的WebAPI控制器
var params = {
provider: "facebook"
};
$.ajax({
url: "http://localhost/taskpro/api/account/ExternalLogin",
data: JSON.stringify(params),
type: "POST",
contentType: 'application/json; charset=utf-8'
})
.done(function (response) {
alert("Success");
});
調用下面的WebAPI控制器:
public class AccountController : ApiController
{
[HttpPost]
[AllowAnonymous]
public bool ExternalLogin(string provider)
{
return true;
}
}
具有以下的RouteMap:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
當我執行這個,小提琴手正在返回:
{"Message":"No HTTP resource was found that matches the request URI
'http://localhost/taskpro/api/account/ExternalLogin'.","MessageDetail":
"No action was found on the controller 'Account' that matches the request."}
我有幾個其他調用正在這個控制器工作得很好。只是這個電話給我帶來麻煩。
而且,如果我刪除了參數的控制器,它只是
public bool ExternalLogin()
和註釋掉AJAX數據線,它工作正常。
任何想法爲什麼路由不能用於此調用?
只是fyi ..默認'string'數據類型綁定來自Uri,所以在這種情況下,Web API試圖在Uri(路由數據和查詢字符串參數)中找到名爲'provider'的鍵的值... so FromBody裝飾是必需的。 –
我錯過了FromBody語句時也有同樣的問題,謝謝! –