1
我有一個API在本地正常工作,當我將它移動到實際環境時,它不會。WebAPI控制器不工作,而另一個工作
受影響的控制器返回主哨行動:
NotFound
與測試GET動作,我回去:
"Message": "No HTTP resource was found that matches the request URI
奇怪的是,當我上傳了的TestController具有相同的測試行動在主控制器中使用,我從API獲得適當的響應。
這是正常工作的測試:不工作
public class TestController : ApiController
{
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage helloWorld()
{
return Request.CreateResponse(HttpStatusCode.OK, "HelloWorld!");
}
}
控制器:
public class DeviceController : ApiController
{
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage helloWorld() // This returns: "No HTTP resource was found that matches the request URI 'http://api.mySite.com/api/Device/helloWorld'."
{
return Request.CreateResponse(HttpStatusCode.OK, "HelloWorld!");
}
[AllowAnonymous]
[HttpPost]
public HttpResponseMessage Login([FromBody] LoginObject loginObject) // This returns: "NotFound"
{
...
}
}
這裏是web配置:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
所以'HTTP:// api.mySite.com/API /測試/ helloWorld'工作,對? – Marusyk
是的,我得到「HelloWorld!」背部。這似乎很奇怪...是一個路由問題? – Ryan
嗯...讓我們檢查一下。嘗試添加路由屬性'[Route(「api/Device/helloWorld」)]' – Marusyk