14

Is there a Web API controller method equivalent to the MVC controller method RedirectToAction? I would like to call one method from another, but retain the filter actions for the secondary method.

asked Jun 27, 2013 at 15:47
2
  • This might answer your question: stackoverflow.com/questions/16295597/… Commented Jun 27, 2013 at 15:50
  • The different actions have different parameters. How would I be able to set the parameters on the secondary action using a header based redirect? Making a direct class call would not allow me to make use of the existing action filters. Or am I approaching this incorrectly? Should I instead be looking to custom routing, instead? Commented Jun 27, 2013 at 16:14

2 Answers 2

30

Is there a Web API controller method equivalent to the MVC controller method RedirectToAction?

You could set the Location header:

public HttpResponseMessage Get()
{
 var response = Request.CreateResponse(HttpStatusCode.Found);
 response.Headers.Location = new Uri("http://www.google.com");
 return response;
}
answered Jun 27, 2013 at 16:01

3 Comments

The two actions have different parameters. How would I be able to set the values for the parameters for the secondary action using the header redirect?
Just use the Url helper: response.Headers.Location = new Uri(Url.Action("SomeAction", "SomeController", new { foo = "bar" }));.
This was very close to satisfying my needs. My case, however, requires the variable to be a POST. If that were not the case, this solution would prove very interesting (though WebAPI doesn't have Url.Action). Nonetheless, this is by far the best answer....
6

You can check this for redirecting a page from a controller in web api

[Route("Report/MyReport")]
public async Task<IHttpActionResult> getReport() {
 string url = "https://localhost:44305/Templates/ReportPage.html";
 System.Uri uri = new System.Uri(url);
 return Redirect(uri);
}
Haroen Viaene
1,36618 silver badges35 bronze badges
answered Feb 17, 2016 at 5:47

2 Comments

Edit your last answer rather than posting a new one
i have deleted that.

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.