return new RedirectResult() vs return Redirect()

39,782

Solution 1

straight from the source

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System.Diagnostics.CodeAnalysis;
using System.Web.Mvc.Properties;

namespace System.Web.Mvc
{
    // represents a result that performs a redirection given some URI
    public class RedirectResult : ActionResult
    {
        [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#", Justification = "Response.Redirect() takes its URI as a string parameter.")]
        public RedirectResult(string url)
            : this(url, permanent: false)
        {
        }

        [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#", Justification = "Response.Redirect() takes its URI as a string parameter.")]
        public RedirectResult(string url, bool permanent)
        {
            if (String.IsNullOrEmpty(url))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "url");
            }

            Permanent = permanent;
            Url = url;
        }

        public bool Permanent { get; private set; }

        [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "Response.Redirect() takes its URI as a string parameter.")]
        public string Url { get; private set; }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (context.IsChildAction)
            {
                throw new InvalidOperationException(MvcResources.RedirectAction_CannotRedirectInChildAction);
            }

            string destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext);
            context.Controller.TempData.Keep();

            if (Permanent)
            {
                context.HttpContext.Response.RedirectPermanent(destinationUrl, endResponse: false);
            }
            else
            {
                context.HttpContext.Response.Redirect(destinationUrl, endResponse: false);
            }
        }
    }
}

The second argument determines whether the response is a 302 (temporary) or 301 permanent redirection. By default, the value is false.

The second method is on Controller and is simply a convenience method. This method has been around for a number of versions of MVC (as far back as at least 2), but IIRC, the addition of the Permanent part to RedirectResult I think has come in in MVC 4 (I don't recall seeing it in MVC 3).

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Security.Principal;
using System.Text;
using System.Web.Mvc.Async;
using System.Web.Mvc.Properties;
using System.Web.Profile;
using System.Web.Routing;
namespace System.Web.Mvc
{
    [SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Class complexity dictated by public surface area")]
    public abstract class Controller : ControllerBase, IActionFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter, IAsyncController, IAsyncManagerContainer
    {
      // omitted for brevity

      [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#", Justification = "Response.Redirect() takes its URI as a string parameter.")]
      protected internal virtual RedirectResult Redirect(string url)
      {
          if (String.IsNullOrEmpty(url))
          {
              throw new ArgumentException(MvcResources.Common_NullOrEmpty, "url");
          }

          return new RedirectResult(url);
      }
    }
}

Solution 2

this.Redirect(string url) - It will internally create new object of RedirectResult class and do temporary redirection.

new RedirectResult(string url, bool permanent) - It will redirect but gives you an option to redirect permanently or temporary.

Solution 3

They do the same thing. The Redirect method of the controller creates a new RedirectResult. If you instantiate the RedirectResult you also have the ability to add a parameter which determines whether the redirect is permanent (or not).

Share:
39,782
Curtis
Author by

Curtis

https://curtiscode.dev

Updated on June 19, 2020

Comments

  • Curtis
    Curtis almost 4 years

    What is the difference between the following two controller ActionResult return statements:

    return new RedirectResult("http://www.google.com", false);
    

    and

    return Redirect("http://www.google.com");
    
  • ek_ny
    ek_ny over 11 years
    @Curt - I'm not sure on that one. I would assume that the controller's Redirect() Method will result in a temporary redirect-- while the overloads of the RedirectResult() constructor give you more control over permanent/temporary. I would think that the Redirect() method of the controller was put in to make things easier-- you don't have to explicity construct the redirect result-- similar to the controllers View() method. So I wouldn't think it's legacy.
  • MiniRagnarok
    MiniRagnarok almost 11 years
    Just so you know, permanent redirect for RedirectResult is in MVC 3.