Asp.net
返回新的 RedirectResult() 與返回 Redirect()
以下兩個控制器 ActionResult 返回語句有什麼區別:
return new RedirectResult("http://www.google.com", false);和
return Redirect("http://www.google.com");
直接從**源頭**
// 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); } } } }第二個參數確定響應是 302(臨時)還是 301 永久重定向。預設情況下,該值為
false。第二種方法是打開
Controller的,只是一種方便的方法。這種方法已經出現在 MVC 的多個版本中(至少可以追溯到 2 個版本),但是 IIRC,RedirectResult我認為在 MVC 4 中添加了永久部分(我不記得在 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); } } }