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
-
This might answer your question: stackoverflow.com/questions/16295597/…Dennis Traub– Dennis Traub2013年06月27日 15:50:36 +00:00Commented 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?Eric– Eric2013年06月27日 16:14:14 +00:00Commented Jun 27, 2013 at 16:14
2 Answers 2
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
Eric
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?
Darin Dimitrov
Just use the
Url
helper: response.Headers.Location = new Uri(Url.Action("SomeAction", "SomeController", new { foo = "bar" }));
.Eric
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....
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
Moumit
Edit
your last answer rather than posting a new one
Debendra Dash
i have deleted that.