Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect in a .NET API action filter

I'd like to be able to read a Request header in an ActionFilterAttribute, and direct the user. I'd also like to maintain the existing Request, or pass the controller and URL params to the new Request. I know this is easy in MVC, but haven't done it in a web API.

like image 376
dylanthelion Avatar asked Jul 06 '17 21:07

dylanthelion


1 Answers

Actually it is very easy. You just create HttpResponseMessage object.

public class RedirectAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var response = actionContext.Request.CreateResponse(HttpStatusCode.Redirect);
            response.Headers.Location = new Uri("https://www.stackoverflow.com");
            actionContext.Response = response;
        }
    }
like image 79
Ivan R. Avatar answered Sep 29 '22 06:09

Ivan R.