Shahdat
Shahdat

Reputation: 5483

Redirect from asp.net web api post action

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?

Upvotes: 127

Views: 147835

Answers (6)

David
David

Reputation: 535

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

app.MapGet("/", () => Results.Redirect("/swagger"));

Upvotes: 2

widavies
widavies

Reputation: 986

[HttpGet]
public RedirectResult Get() {
  return new RedirectResult("https://example.com");
}

Upvotes: 1

Jigar Mistri
Jigar Mistri

Reputation: 171

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

Upvotes: 17

Debendra Dash
Debendra Dash

Reputation: 5636

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);
}

Upvotes: 9

Syed Ali
Syed Ali

Reputation: 1897

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

Upvotes: 28

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

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;
}

Upvotes: 237

Related Questions