Asp.net 返回新的重定向结果()与返回重定向()比较
以下两个控制器ActionResult返回语句之间的区别是什么:Asp.net 返回新的重定向结果()与返回重定向()比较,asp.net,asp.net-mvc,asp.net-mvc-3,redirect,actionresult,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Redirect,Actionresult,以下两个控制器ActionResult返回语句之间的区别是什么: return new RedirectResult("http://www.google.com", false); 及 他们做同样的事情。控制器的重定向方法创建一个新的重定向结果。如果实例化RedirectResult,您还可以添加一个参数来确定重定向是否是永久性的。直接从 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See L
return new RedirectResult("http://www.google.com", false);
及
他们做同样的事情。控制器的重定向方法创建一个新的重定向结果。如果实例化RedirectResult,您还可以添加一个参数来确定重定向是否是永久性的。直接从
// 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);
}
}
}
}
第二个参数确定。默认情况下,该值为false
第二种方法是在Controller
上,这是一种简单方便的方法。这种方法已经出现在许多版本的MVC中(至少早在2个版本),但是IIRC,即在RedirectResult
中添加永久性部分,我想在MVC4中已经出现了(我不记得在MVC3中见过)
this.Redirect(字符串url)-它将在内部创建RedirectResult类的新对象并执行临时重定向
新建重定向结果(字符串url,bool permanent)-它将重定向,但为您提供永久或临时重定向的选项。@Curt-我不确定这一点。我假设控制器的Redirect()方法将导致临时重定向,而RedirectResult()构造函数的重载使您能够更好地控制永久/临时重定向。我认为控制器的Redirect()方法是为了使事情变得更简单——您不必显式地构造重定向结果——类似于controllers View()方法。所以我不认为这是传统。正如你们所知,重定向结果的永久重定向在MVC3中。
// 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);
}
}
}
}
// 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);
}
}
}