Reputation: 1288
My api controller need to implement a simple GET action using 2 parameters: an ItemID and a SectionID. The return type ItemInSection contains data about the item in the section.
A same item can be in multiple sections. It's why the SectionID is required.
If the item has been moved and is no longer in the corresponding section, I need to return a redirect code 301 with a location url corresponding to one of the sections containing the item.
What is the best way to do this?
For other errors (eg 404, 401 ...) code, I use HttpResponseException. But for 301 case, how to specify the redirection url?
Upvotes: 2
Views: 4144
Reputation: 1288
The solution is simply to use the HttpResponseException(HttpResponseMessage response)
constructor (instead of the constructor taken a simple HttpStatusCode
).
Just write:
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Moved);
response.Headers.Location = ...;
throw new HttpResponseException(response);
Upvotes: 6