asp.net-mvc 从asp.net web api post操作重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11324711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:04:55  来源:igfitidea点击:

Redirect from asp.net web api post action

asp.net-mvcc#-4.0asp.net-mvc-4asp.net-web-api

提问by Shahdat

I'm very new to ASP.NET 4.0 Web API. Can we redirect to another URL at the end of the POST action?, something like ... Response.Redirect(url)

我对 ASP.NET 4.0 Web API 很陌生。我们可以在 POST 操作结束时重定向到另一个 URL 吗?,类似于 ...Response.Redirect(url)

Actually I upload file from a MVC application (say www.abcmvc.com) through Web API (say www.abcwebapi.com/upload)

实际上,我www.abcmvc.com通过 Web API(比如说www.abcwebapi.com/upload)从 MVC 应用程序(比如说)上传文件

Here uploadis the POST action. I post a multi-part form to Web API upload controller's post action. After uploading I would like to redirect back to www.abcmvc.com.

upload是 POST 操作。我将多部分表单发布到 Web API 上传控制器的发布操作。上传后,我想重定向回www.abcmvc.com.

Is this possible?

这可能吗?

回答by Darin Dimitrov

Sure:

当然:

public HttpResponseMessage Post()
{
    // ... do the job

    // now redirect
    var response = Request.CreateResponse(HttpStatusCode.Moved);
    response.Headers.Location = new Uri("http://www.abcmvc.com");
    return response;
}

回答by sttaq

Here is another way you can get to the root of your website without hard coding the url:

这是另一种无需硬编码网址即可到达网站根目录的方法:

var response = Request.CreateResponse(HttpStatusCode.Moved);
string fullyQualifiedUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority);
response.Headers.Location = new Uri(fullyQualifiedUrl);

Note: Will only work if both your MVC website and WebApi are on the same URL

注意: 仅当您的 MVC 网站和 WebApi 位于同一 URL 时才有效

回答by Jigar Mistri

    [HttpGet]
    public RedirectResult Get()
    {
        return RedirectPermanent("https://www.google.com");
    }

回答by Debendra Dash

You can check this

你可以检查这个

[Route("Report/MyReport")]
public IHttpActionResult GetReport()
{

   string url = "https://localhost:44305/Templates/ReportPage.html";

   System.Uri uri = new System.Uri(url);

   return Redirect(uri);
}