Redirect from asp.net web api post action

前端 未结 4 845
深忆病人
深忆病人 2020-11-27 14:05

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

相关标签:
4条回答
  • 2020-11-27 14:09

    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);
    }
    
    0 讨论(0)
  • 2020-11-27 14:13

    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

    0 讨论(0)
  • 2020-11-27 14:21

    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;
    }
    
    0 讨论(0)
  • 2020-11-27 14:33
        [HttpGet]
        public RedirectResult Get()
        {
            return RedirectPermanent("https://www.google.com");
        }
    
    0 讨论(0)
提交回复
热议问题