我有一个在本地运行良好的API,当我将它移动到实时环境时,它没有。
受影响控制器上的主POST操作返回:
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"
{
...
}
}
这是网络配置:
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 }
);
}
}
答案 0 :(得分:1)
尝试添加显式声明路由,如acrion
[Route("api/Device/helloWorld")]
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage helloWorld()
或
[RoutePrefix("api/Device")]
public class DeviceController : ApiController
然后
[Route("helloWorld")]
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage helloWorld()
答案 1 :(得分:0)
对于将来像我这样的可怜树汁:确保控制器上的方法是公开的。