2013-02-14 7 views

Antwort

19

direkt aus dem 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); 
      } 
     } 
    } 
} 

Das zweite Argument bestimmt, ob die response is a 302 (temporary) or 301 permanent redirection. Standardmäßig lautet der Wert false. Die zweite Methode ist Controller und ist einfach eine bequeme Methode. Diese Methode ist für eine Anzahl von Versionen von MVC (mindestens 2) verfügbar, aber IIRC, der Zusatz des permanenten Teils zu RedirectResult Ich denke, ist in MVC 4 gekommen (ich erinnere mich nicht, es in zu sehen 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); 
     } 
    } 
} 
+1

Nur damit Sie wissen, permanente Umleitung für RedirectResult _is_ in MVC 3. – MiniRagnarok

3

Sie das gleiche tun. Die Redirect-Methode des Controllers erstellt ein neues RedirectResult. Wenn Sie das RedirectResult instanziieren, haben Sie auch die Möglichkeit, einen Parameter hinzuzufügen, der bestimmt, ob die Weiterleitung dauerhaft ist (oder nicht).

+0

@ Curt - Ich bin mir da nicht sicher. Ich würde annehmen, dass die Redirect() - Methode des Controllers zu einer temporären Weiterleitung führen wird - während die Überladungen des RedirectResult() - Konstruktors Ihnen mehr Kontrolle über permanente/temporäre geben. Ich würde denken, dass die Redirect() - Methode des Controllers eingefügt wurde, um die Dinge einfacher zu machen - Sie müssen nicht explizit das Redirect-Ergebnis konstruieren - ähnlich wie die View() - Methode der Controller. Also würde ich nicht glauben, dass es Legacy ist. –

+0

+1 danke @ek_ny – Curt

5

this.Redirect (string url) - Es wird intern neues Objekt der RedirectResult Klasse erstellen und temporäre Umleitung tun.

neu RedirectResult (string url, bool permanent) - Es wird umgeleitet, sondern gibt Ihnen die Möglichkeit, dauerhaft oder vorübergehend umgeleitet werden.