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.

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;
}
Sign up to request clarification or add additional context in comments.

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);
}

2 Comments

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

Your Answer

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.