My API has the following routes
GET: api / department
GET: api / department / {departmentID} / employees
The second route displays the next controller action
public IEnumerable<Employee> Get(int departmentID)
{
return GetEmployees(departmentID);
}
This route could potentially be called using a department identifier that does not exist or the user does not have access to access. When this happens, what is the right way to handle this? Currently, I have changed the action of my controller to return 403, as shown below.
public HttpResponseMessage Get(int departmentID)
{
var isDepartmentValid = CheckIfDepartmentIsAccessible(username, departmentID);
if(!isDepartmentValid)
{
return Request.CreateResponse(HttpStatusCode.Forbidden);
}
Request.CreateResponse(HttpStatusCode.OK, GetEmployees(departmentID));
}
Is this right to do? It seems that changing the method signature makes it difficult to understand the type of content returned from the controller action. Is there a way for the method signature to remain the same, but return 403 if necessary?
user1625066