WebFX 156 - Adding support for Redirect and RedirectPermanent

This commit is contained in:
sornaks 2014-03-31 14:56:50 -07:00 committed by Sornakumar
parent 3548a46ca9
commit 7c8dd45b8b
5 changed files with 140 additions and 5 deletions

View File

@ -0,0 +1,38 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.Mvc.Core;
namespace Microsoft.AspNet.Mvc
{
public class RedirectResult : IActionResult
{
public RedirectResult(string url)
: this(url, permanent: false)
{
}
public RedirectResult(string url, bool permanent)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, "url");
}
Permanent = permanent;
Url = url;
}
public bool Permanent { get; private set; }
public string Url { get; private set; }
public async Task ExecuteResultAsync([NotNull] ActionContext context)
{
// It is redirected directly to the input URL.
// We would use the context to construct the full URL,
// only when relative URLs are supported. (Issue - WEBFX-202)
context.HttpContext.Response.Redirect(Url, Permanent);
}
}
}

View File

@ -1,5 +1,7 @@
using System.Text;
using System;
using System.Text;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Mvc.Core;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
@ -97,5 +99,25 @@ namespace Microsoft.AspNet.Mvc
{
return Result.Json(value);
}
public virtual RedirectResult Redirect(string url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, "url");
}
return new RedirectResult(url);
}
public virtual RedirectResult RedirectPermanent(string url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, "url");
}
return new RedirectResult(url, permanent: true);
}
}
}

View File

@ -250,6 +250,22 @@ namespace Microsoft.AspNet.Mvc.Core
return string.Format(CultureInfo.CurrentCulture, GetString("DefaultControllerFactory_ActionDescriptorMustBeReflected"), p0);
}
/// <summary>
/// The value cannot be null or empty.
/// </summary>
internal static string ArgumentCannotBeNullOrEmpty
{
get { return GetString("ArgumentCannotBeNullOrEmpty"); }
}
/// <summary>
/// The value cannot be null or empty.
/// </summary>
internal static string FormatArgumentCannotBeNullOrEmpty()
{
return GetString("ArgumentCannotBeNullOrEmpty");
}
/// <summary>
/// The '{0}' property of '{1}' must not be null.
/// </summary>
@ -267,7 +283,7 @@ namespace Microsoft.AspNet.Mvc.Core
}
/// <summary>
/// The '{0}' must return a non null '{1}'.
/// The '{0}' must return a non-null '{1}'.
/// </summary>
internal static string MethodMustReturnNotNullValue
{
@ -275,7 +291,7 @@ namespace Microsoft.AspNet.Mvc.Core
}
/// <summary>
/// The '{0}' must return a non null '{1}'.
/// The '{0}' must return a non-null '{1}'.
/// </summary>
internal static string FormatMethodMustReturnNotNullValue(object p0, object p1)
{
@ -287,7 +303,7 @@ namespace Microsoft.AspNet.Mvc.Core
var value = _resourceManager.GetString(name);
System.Diagnostics.Debug.Assert(value != null);
if (formatterNames != null)
{
for (var i = 0; i < formatterNames.Length; i++)

View File

@ -162,6 +162,9 @@
<data name="DefaultControllerFactory_ActionDescriptorMustBeReflected" xml:space="preserve">
<value>The action descriptor must be of type '{0}'.</value>
</data>
<data name="ArgumentCannotBeNullOrEmpty" xml:space="preserve">
<value>The value cannot be null or empty.</value>
</data>
<data name="PropertyOfTypeCannotBeNull" xml:space="preserve">
<value>The '{0}' property of '{1}' must not be null.</value>
</data>

View File

@ -1,5 +1,7 @@
using Microsoft.AspNet.Mvc.ModelBinding;
using System;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Testing;
using Xunit;
namespace Microsoft.AspNet.Mvc.Core
@ -27,5 +29,59 @@ namespace Microsoft.AspNet.Mvc.Core
Assert.Equal("property", controller.ViewBag.Another);
Assert.Equal("property", controller.ViewData["Another"]);
}
[Fact]
public void Redirect_Temporary_SetsSameUrl()
{
// Arrange
var controller = new Controller();
// Act
var result = controller.Redirect("sample\\url");
// Assert
Assert.False(result.Permanent);
Assert.Equal("sample\\url", result.Url);
}
[Fact]
public void Redirect_Permanent_SetsSameUrl()
{
// Arrange
var controller = new Controller();
// Act
var result = controller.RedirectPermanent("sample\\url");
// Assert
Assert.True(result.Permanent);
Assert.Equal("sample\\url", result.Url);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void Redirect_NullOrEmptyUrl_Throws(string url)
{
// Arrange
var controller = new Controller();
// Act & Assert
ExceptionAssert.ThrowsArgument(
() => controller.Redirect(url: url), "url", "The value cannot be null or empty");
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void RedirectPermanent_NullOrEmptyUrl_Throws(string url)
{
// Arrange
var controller = new Controller();
// Act & Assert
ExceptionAssert.ThrowsArgument(
() => controller.RedirectPermanent(url: url), "url", "The value cannot be null or empty");
}
}
}