2017-01-16 68 views
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 } 

     ); 
    } 
} 
+0

所以'HTTP:// api.mySite.com/API /測試/ helloWorld'工作,對? – Marusyk

+0

是的,我得到「HelloWorld!」背部。這似乎很奇怪...是一個路由問題? – Ryan

+0

嗯...讓我們檢查一下。嘗試添加路由屬性'[Route(「api/Device/helloWorld」)]' – Marusyk

回答

1

嘗試添加顯式LY申報航線的像由acrion

[Route("api/Device/helloWorld")] 
[AllowAnonymous] 
[HttpGet] 
public HttpResponseMessage helloWorld() 

[RoutePrefix("api/Device")] 
public class DeviceController : ApiController 

然後

[Route("helloWorld")] 
[AllowAnonymous] 
[HttpGet] 
public HttpResponseMessage helloWorld() 
+0

明確聲明路線爲我解決了這個問題,謝謝。 – Ryan

相關問題