VS回报重定向返回新RedirectResult()()(return new RedirectRe

2019-07-19 21:12发布

什么是以下两个控制器的ActionResult return语句之间的区别:

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

return Redirect("http://www.google.com");

Answer 1:

直接从

// 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);
      }
    }
}


Answer 2:

this.Redirect(字符串URL) -它会在内部创建RedirectResult类的新对象,并做临时重定向。

新RedirectResult(字符串URL,布尔永久) -它会重定向但给你一个选择永久或临时重定向。



Answer 3:

他们做同样的事情。 控制器的重定向方法创建一个新RedirectResult。 如果实例化RedirectResult还必须添加它决定了重定向是否是永久性的(或不)的参数的能力。



文章来源: return new RedirectResult() vs return Redirect()