Added RedirectToAction overload with no arguments

This commit is contained in:
Kiran Challa 2017-09-01 13:59:13 -07:00
parent 06f6de6c11
commit 4f18d99d02
3 changed files with 60 additions and 0 deletions

View File

@ -456,6 +456,36 @@ namespace Microsoft.AspNetCore.Mvc
return new LocalRedirectResult(localUrl: localUrl, permanent: true, preserveMethod: true);
}
/// <summary>
/// Redirects (<see cref="StatusCodes.Status302Found"/>) to an action with the same name as current one.
/// The 'controller' and 'action' names are retrieved from the ambient values of the current request.
/// </summary>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
/// <example>
/// A POST request to an action named "Product" updates a product and redirects to an action, also named
/// "Product", showing details of the updated product.
/// <code>
/// [HttpGet]
/// public IActionResult Product(int id)
/// {
/// var product = RetrieveProduct(id);
/// return View(product);
/// }
///
/// [HttpPost]
/// public IActionResult Product(int id, Product product)
/// {
/// UpdateProduct(product);
/// return RedirectToAction();
/// }
/// </code>
/// </example>
[NonAction]
public virtual RedirectToActionResult RedirectToAction()
{
return RedirectToAction(actionName: null);
}
/// <summary>
/// Redirects (<see cref="StatusCodes.Status302Found"/>) to the specified action using the <paramref name="actionName"/>.
/// </summary>

View File

@ -402,6 +402,24 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
Assert.Equal(expected, response.Trim());
}
[Fact]
public async Task RedirectToAction_WithEmptyActionName_UsesAmbientValue()
{
// Arrange
var product = new List<KeyValuePair<string, string>>();
product.Add(new KeyValuePair<string, string>("SampleInt", "20"));
// Act
var response = await Client.PostAsync("/Home/Product", new FormUrlEncodedContent(product));
// Assert
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.NotNull(response.Headers.Location);
Assert.Equal("/Home/Product", response.Headers.Location.ToString());
var responseBody = await Client.GetStringAsync("/Home/Product");
Assert.Equal("Get Product", responseBody);
}
[Fact]
public async Task ActionMethod_ReturningActionMethodOfT_WithBadRequest()
{

View File

@ -108,5 +108,17 @@ namespace BasicWebSite.Controllers
{
return ControllerContext.ActionDescriptor.Properties["description"].ToString();
}
[HttpGet]
public IActionResult Product()
{
return Content("Get Product");
}
[HttpPost]
public IActionResult Product(Product product)
{
return RedirectToAction();
}
}
}