21

次の2つのコントローラーActionResultreturnステートメントの違いは何ですか。

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

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

3 に答える 3

21

ソースから直接

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

2番目の引数は、応答が302(一時的)または301永続的リダイレクトのどちらであるかを決定します。デフォルトでは、値はfalseです。

2番目の方法はオンControllerであり、単に便利な方法です。この方法は、MVCの多くのバージョン(少なくとも2つまでさかのぼります)で使用されてきましたが、IIRC、永続的な部分の追加はRedirectResultMVC 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);
      }
    }
}
于 2013-02-14T13:32:51.747 に答える
8

this.Redirect(string url) - RedirectResult クラスの新しいオブジェクトを内部的に作成し、一時的なリダイレクトを行います。

new RedirectResult(string url, bool permanent) - リダイレクトしますが、永続的または一時的にリダイレクトするオプションを提供します。

于 2015-02-07T08:50:38.890 に答える
3

彼らは同じことをします。コントローラのRedirectメソッドは、新しいRedirectResultを作成します。RedirectResultをインスタンス化する場合、リダイレクトが永続的であるかどうかを決定するパラメーターを追加することもできます。

于 2013-02-14T13:30:09.603 に答える