locked
301 Redirect as a Action Result RRS feed

  • Question

  • User2079652561 posted

     I just wonder what you think it the "proper" way to perform a 301 Redirect from a Controller? I can NOT set up Routes in Global.asax because the URL comes from a database. My current approach is this:

     

            [HandleError]
            public ActionResult Resolve(string id)
            {
                if (string.IsNullOrEmpty(id))
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    string url = DatabaseFunctions.RetreiveUrl(id);
                    if (url == null)
                    {
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        Response.StatusCode = 301;
                        Response.RedirectLocation = url;
                        return new ContentResult();
                    }
                }
            }

     

    That works - i just wonder if this is the proper way to do it, or if there is a more "MVC-endorsed" approach?

    Wednesday, October 22, 2008 5:09 PM

All replies

  • User-1660457439 posted

    return Redirect(url);

    Just call that helper from your action method.

    Wednesday, October 22, 2008 6:06 PM
  • User2079652561 posted

    Thanks, but unfortunately Controller.Redirect returns a 302 instead of a 301. Before I now drive into Reflector to make my own custom ActionResult, may I suggest changing Controller.Redirect to let the user choose whether to return a 301 or a 302? (or even a 303, even though nobody seems to use that)

    Wednesday, October 22, 2008 6:16 PM
  • User-1660457439 posted

    Under the covers we're just using Response.Redirect(), which is what's setting the HTTP 302.  We have no control over this.  As for adding yet another overload to all of our RedirectXyz() methods, I don't think that has a high chance of happening.  The demand simply isn't there, and it's simple to create a specialized ActionResult for this.

    Wednesday, October 22, 2008 6:26 PM
  • User2079652561 posted

     Thanks. That's what I have done now, created a custom ActionResult:

         public class PermanentRedirectResult : ActionResult
        {
            public string Url { get; set; }

            public PermanentRedirectResult(string url)
            {
                if (string.IsNullOrEmpty(url))
                {
                    throw new ArgumentException("url is null or empty", "url");
                }
                this.Url = url;
            }

            public override void ExecuteResult(ControllerContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }
                context.HttpContext.Response.StatusCode = 301;
                context.HttpContext.Response.RedirectLocation = Url;
                context.HttpContext.Response.End();
            }
        }

    Wednesday, October 22, 2008 6:27 PM
  • User1030240076 posted

    Excellent! My little touch... to be cleaner I would use

    public string Url { get; private set; }

    Thanks, well done!

    Sunday, January 29, 2012 7:38 PM