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");
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");
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);
}
}
}
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.
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).