127

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)

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

Here upload is 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.

Is this possible?

SteveC
17k25 gold badges115 silver badges180 bronze badges
asked Jul 4, 2012 at 7:53

6 Answers 6

237

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;
}
answered Jul 4, 2012 at 8:02

2 Comments

Using this Redirect technique solved the "object moved to" WebAPI page I was getting with other redirect techniques. Also for Redirect temporary instead of permanent you can use HttpStatusCode.Redirect (302) or .RedirectMethod (303)
@Darin Dimitrov, this works. Why is it that when I use HttpStatusCode.Redirect instead, my client receives a 401 response?
28

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

answered Jun 30, 2013 at 14:14

Comments

17
 [HttpGet]
 public RedirectResult Get()
 {
 return RedirectPermanent("https://www.google.com");
 }
answered Sep 18, 2017 at 9:34

Comments

9

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);
}
Sudhanshu Mishra
6,7932 gold badges63 silver badges77 bronze badges
answered Feb 17, 2016 at 5:52

2 Comments

@dotnetguy Please stop suggesting the same edit. If you want, post the code you changed in a separate answer. There is no point in continuing to edit, the reviewers will turn each edit down.
Redirect get a string arg ,according microsoft: learn.microsoft.com/en-us/dotnet/api/…
2

If you use ASP.NET Web API you can just use the following

app.MapGet("/", () => Results.Redirect("/swagger"));
answered Mar 11, 2023 at 20:37

Comments

1
[HttpGet]
public RedirectResult Get() {
 return new RedirectResult("https://example.com");
}
answered Nov 18, 2022 at 5:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.