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");
The View() method doesn't make new requests, it just renders the view without changing URLs in the browser's address bar. The RedirectToAction() method makes new requests and URL in the browser's address bar is updated with the generated URL by MVC.
RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.
return RedirectToAction()To redirect to a different action which can be in the same or different controller. It tells ASP.NET MVC to respond with a browser to a different action instead of rendering HTML as View() method does. Browser receives this notification to redirect and makes a new request for the new action.
RedirectToRouteResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header. Targets a registered route. It should be used when we want to redirect to a route.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With