How to Redirect in ASPNET Web API

2020-02-26 06:55发布

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条回答
劫难
2楼-- · 2020-02-26 07:27

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);
}
查看更多
Viruses.
3楼-- · 2020-02-26 07:28

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;
}
查看更多
登录 后发表回答