2

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?

asked Feb 13, 2014 at 16:40
2
  • 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. Commented Feb 13, 2014 at 16:50
  • In my view, impose two successive calls to the API is not a REST architecture. Commented Feb 17, 2014 at 11:22

1 Answer 1

6

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);
answered Feb 20, 2014 at 14:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.