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