Merge branch 'WebFX156' into dev

This commit is contained in:
Sornakumar 2014-04-08 10:52:38 -07:00
commit b25091f6c1
5 changed files with 139 additions and 4 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>
@ -495,7 +511,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>
@ -207,4 +210,4 @@
<data name="ViewData_WrongTModelType" xml:space="preserve">
<value>The model item passed into the ViewDataDictionary is of type '{0}', but this ViewDataDictionary instance requires a model item of type '{1}'.</value>
</data>
</root>
</root>

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