Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create 303 Response in asp.net

Does anyone know how to redirect current request in ASP.NET using http status code 303 (SeeOther).

Code snippets are more than welcome!

like image 772
ni5ni6 Avatar asked Feb 29 '12 10:02

ni5ni6


2 Answers

You should be able to do it like this:

  HttpContext.Current.Response.Clear();
  HttpContext.Current.Response.Status = "303 See Other";
  HttpContext.Current.Response.AddHeader("Location", newLocation);
  HttpContext.Current.Response.End();
like image 115
Klaus Byskov Pedersen Avatar answered Oct 19 '22 18:10

Klaus Byskov Pedersen


This is a .NetCore / .Net5.0+ implementation as an extension method.

public static class ControllerExtensions
{
    public static ActionResult SeeOther(this Controller controller, string location)
    {
        controller.Response.Headers.Add("Location", location);
        return new StatusCodeResult(303);
    }
}

Usage From Controller: return this.SeeOther("/resource-endpoint");

like image 43
N-ate Avatar answered Oct 19 '22 17:10

N-ate