2016-04-12 67 views
1

我在下面的代碼中得到了「不是所有代碼路徑都返回值」。我有下面的代碼。我想我會適當地回來,但仍然有錯誤。並非所有的代碼路徑在使用Try TryCatch時都返回值

[Route("User")] 
public HttpResponseMessage Post([FromBody] Employee employee) 
//FromBody forces the web api to read a simple tye from the request body. 
{ 
    try 
    { 
     Employee incomingEmployee = employee; 
     if (incomingEmployee == null) 
     { 
     Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read the request"); 
     } 
     else if (UserManager.AddUser(employee) > 0) 
     { 
     return Request.CreateResponse(HttpStatusCode.Created); 
     } 
     else 
     { 
     return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save to database"); 
     } 
    } 
    catch (Exception ex) 
    { 
     return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); 
    } 
} 

回答

1

您在第一個if語句中忘記了return語句。

[Route("User")] 
public HttpResponseMessage Post([FromBody] Employee employee) 
//FromBody forces the web api to read a simple tye from the request body. 
{ 
    try 
    { 
     Employee incomingEmployee = employee; 
     if (incomingEmployee == null) 
     { 
    -->return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read the request"); 
     } 
     else if (UserManager.AddUser(employee) > 0) 
     { 
     return Request.CreateResponse(HttpStatusCode.Created); 
     } 
     else 
     { 
     return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save to database"); 
     } 
    } 
    catch (Exception ex) 
    { 
     return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); 
    } 
} 
+0

我不敢相信我沒有看到....非常感謝你 – Virodh

相關問題