我有一個繼承自ApiController
的類,它的一些方法調用正確,其他一些方法是Not found
。我找不到原因。我一直在尋找一個解決方案几個小時,現在仍然沒有得到它。請注意,我是新手,這是我在C#中的第一個WebApi。404在C#上找不到WebApi
路由:(WebApiConfig.cs)
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Configuration et services API Web
// Itinéraires de l'API Web
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
控制器:
public class ExchangeController : ApiController
{
public HttpResponseMessage GetMailHeader(int id)
{
Console.WriteLine(id);
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent("ok");
return response;
}
public HttpResponseMessage GetTest()
{
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent("working !!");
return response;
}
}
JS:
$.ajax({
type: "GET",
url: "/api/exchange/getTest",
done: function (data) {
console.log(data);
}
});
$.ajax({
type: "GET",
url: "/api/exchange/getMailHeader",
data: "42",
done: function (data) {
console.log(data);
}
});
getTest
該方法返回200 OK
而getMailHeader
返回404 Not Found
。我錯過了什麼 ?
平凡的一步,但我發現自己在無意中發現它:重建,那麼所有的解決方案重新部署。 – tomab
getMailHeader的數據不正確。你應該這樣做:data:{id:42}(將它作爲JSON傳遞) –
@Ahmedilyas這實際上是問題的一部分,它使我找到了解決方案! ;) – Elfayer