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?
-
Can you not use the API to check if the item exists? Then, if it has been moved and is no longer in the corresponding section, redirect the user? I'm not sure how to use the HTTP response exception method--perhaps there is a parameter you can use.Ross Brasseaux– Ross Brasseaux2014年02月13日 16:50:54 +00:00Commented Feb 13, 2014 at 16:50
-
In my view, impose two successive calls to the API is not a REST architecture.nlips– nlips2014年02月17日 11:22:19 +00:00Commented Feb 17, 2014 at 11:22
1 Answer 1
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);