Redirect from asp.net web api post action

asp.net MvcC# 4.0asp.net Mvc-4asp.net Web-Api

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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

Solution 2 - asp.net Mvc

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

Solution 3 - asp.net Mvc

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

Solution 4 - asp.net Mvc

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShahdatView Question on Stackoverflow
Solution 1 - asp.net MvcDarin DimitrovView Answer on Stackoverflow
Solution 2 - asp.net MvcSyed AliView Answer on Stackoverflow
Solution 3 - asp.net MvcJigar MistriView Answer on Stackoverflow
Solution 4 - asp.net MvcDebendra DashView Answer on Stackoverflow