如何在ASPNET Web API中重定向

Eri*_*ric 14 asp.net-web-api

是否有一个Web API控制器方法等效于MVC控制器方法RedirectToAction?我想从另一个方法调用一个方法,但保留辅助方法的过滤器操作.

Dar*_*rov 30

是否有一个Web API控制器方法等效于MVC控制器方法RedirectToAction?

您可以设置Location标头:

public HttpResponseMessage Get()
{
    var response = Request.CreateResponse(HttpStatusCode.Found);
    response.Headers.Location = new Uri("http://www.google.com");
    return response;
}
Run Code Online (Sandbox Code Playgroud)


Deb*_*ash 6

您可以检查此信息,以便从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);
}
Run Code Online (Sandbox Code Playgroud)