diff --git a/src/Microsoft.AspNet.Mvc.Core/Controller.cs b/src/Microsoft.AspNet.Mvc.Core/Controller.cs index a0fd6f2c3f..6ae4224016 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Controller.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Controller.cs @@ -19,7 +19,7 @@ namespace Microsoft.AspNet.Mvc private IViewEngine _viewEngine; [NonAction] - public void Initialize(IServiceProvider serviceProvider, IViewEngine viewEngine) + public virtual void Initialize(IServiceProvider serviceProvider, IViewEngine viewEngine) { _serviceProvider = serviceProvider; _viewEngine = viewEngine; @@ -73,27 +73,48 @@ namespace Microsoft.AspNet.Mvc } } + /// + /// Creates a object that renders a view to the response. + /// + /// The created object for the response. [NonAction] - public ViewResult View() + public virtual ViewResult View() { - return View(view: null); + return View(viewName: null); } + /// + /// Creates a object by specifying a . + /// + /// The name of the view that is rendered to the response. + /// The created object for the response. [NonAction] - public ViewResult View(string view) + public virtual ViewResult View(string viewName) { - return View(view, model: null); + return View(viewName, model: null); } + /// + /// Creates a object by specifying a + /// to be rendered by the view. + /// + /// The model that is rendered by the view. + /// The created object for the response. [NonAction] - // TODO #110: May need here and in the overload below. - public ViewResult View(object model) + public virtual ViewResult View(object model) { - return View(view: null, model: model); + return View(viewName: null, model: model); } + /// + /// Creates a object by specifying a + /// and the to be rendered by the view. + /// + /// The name of the view that is rendered to the response. + /// The model that is rendered by the view. + /// The created object for the response. [NonAction] - public ViewResult View(string view, object model) + public virtual ViewResult View(string viewName, object model) { // Do not override ViewData.Model unless passed a non-null value. if (model != null) @@ -103,25 +124,45 @@ namespace Microsoft.AspNet.Mvc return new ViewResult(_serviceProvider, _viewEngine) { - ViewName = view, + ViewName = viewName, ViewData = ViewData, }; } + /// + /// Creates a object by specifying a string. + /// + /// The content to write to the response. + /// The created object for the response. [NonAction] - public ContentResult Content(string content) + public virtual ContentResult Content(string content) { return Content(content, contentType: null); } + /// + /// Creates a object by specifying a string + /// and a content type. + /// + /// The content to write to the response. + /// The content type (MIME type). + /// The created object for the response. [NonAction] - public ContentResult Content(string content, string contentType) + public virtual ContentResult Content(string content, string contentType) { return Content(content, contentType, contentEncoding: null); } + /// + /// Creates a object by specifying a string, + /// a , and . + /// + /// The content to write to the response. + /// The content type (MIME type). + /// The content encoding. + /// The created object for the response. [NonAction] - public ContentResult Content(string content, string contentType, Encoding contentEncoding) + public virtual ContentResult Content(string content, string contentType, Encoding contentEncoding) { var result = new ContentResult { @@ -141,12 +182,24 @@ namespace Microsoft.AspNet.Mvc return result; } + /// + /// Creates a object that serializes the specified object + /// to JSON. + /// + /// The object to serialize. + /// The created that serializes the specified + /// to JSON format for the response. [NonAction] - public JsonResult Json(object value) + public virtual JsonResult Json(object data) { - return new JsonResult(value); + return new JsonResult(data); } + /// + /// Creates a object that redirects to the specified . + /// + /// The URL to redirect to. + /// The created for the response. [NonAction] public virtual RedirectResult Redirect(string url) { @@ -158,6 +211,12 @@ namespace Microsoft.AspNet.Mvc return new RedirectResult(url); } + /// + /// Creates a object with set to true + /// using the specified . + /// + /// The URL to redirect to. + /// The created for the response. [NonAction] public virtual RedirectResult RedirectPermanent(string url) { @@ -169,104 +228,211 @@ namespace Microsoft.AspNet.Mvc return new RedirectResult(url, permanent: true); } + /// + /// Redirects to the specified action using the . + /// + /// The name of the action. + /// The created for the response. [NonAction] - public RedirectToActionResult RedirectToAction(string actionName) + public virtual RedirectToActionResult RedirectToAction(string actionName) { return RedirectToAction(actionName, routeValues: null); } + /// + /// Redirects to the specified action using the + /// and . + /// + /// The name of the action. + /// The parameters for a route. + /// The created for the response. [NonAction] - public RedirectToActionResult RedirectToAction(string actionName, object routeValues) + public virtual RedirectToActionResult RedirectToAction(string actionName, object routeValues) { return RedirectToAction(actionName, controllerName: null, routeValues: routeValues); } + /// + /// Redirects to the specified action using the + /// and the . + /// + /// The name of the action. + /// The name of the controller. + /// The created for the response. [NonAction] - public RedirectToActionResult RedirectToAction(string actionName, string controllerName) + public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName) { return RedirectToAction(actionName, controllerName, routeValues: null); } + /// + /// Redirects to the specified action using the specified , + /// , and . + /// + /// The name of the action. + /// The name of the controller. + /// The parameters for a route. + /// The created for the response. [NonAction] - public RedirectToActionResult RedirectToAction(string actionName, string controllerName, + public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName, object routeValues) { return new RedirectToActionResult(Url, actionName, controllerName, TypeHelper.ObjectToDictionary(routeValues)); } + /// + /// Redirects to the specified action with set to true + /// using the specified . + /// + /// The name of the action. + /// The created for the response. [NonAction] - public RedirectToActionResult RedirectToActionPermanent(string actionName) + public virtual RedirectToActionResult RedirectToActionPermanent(string actionName) { return RedirectToActionPermanent(actionName, routeValues: null); } + /// + /// Redirects to the specified action with set to true + /// using the specified and . + /// + /// The name of the action. + /// The parameters for a route. + /// The created for the response. [NonAction] - public RedirectToActionResult RedirectToActionPermanent(string actionName, object routeValues) + public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, object routeValues) { return RedirectToActionPermanent(actionName, controllerName: null, routeValues: routeValues); } + /// + /// Redirects to the specified action with set to true + /// using the specified and . + /// + /// The name of the action. + /// The name of the controller. + /// The created for the response. [NonAction] - public RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName) + public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName) { return RedirectToActionPermanent(actionName, controllerName, routeValues: null); } + /// + /// Redirects to the specified action with set to true + /// using the specified , , + /// and . + /// + /// The name of the action. + /// The name of the controller. + /// The parameters for a route. + /// The created for the response. [NonAction] - public RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName, + public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName, object routeValues) { return new RedirectToActionResult(Url, actionName, controllerName, TypeHelper.ObjectToDictionary(routeValues), permanent: true); } + /// + /// Redirects to the specified route using the specified . + /// + /// The name of the route. + /// The created for the response. [NonAction] - public RedirectToRouteResult RedirectToRoute(string routeName) + public virtual RedirectToRouteResult RedirectToRoute(string routeName) { return RedirectToRoute(routeName, routeValues: null); } + /// + /// Redirects to the specified route using the specified . + /// + /// The parameters for a route. + /// The created for the response. [NonAction] - public RedirectToRouteResult RedirectToRoute(object routeValues) + public virtual RedirectToRouteResult RedirectToRoute(object routeValues) { return RedirectToRoute(routeName: null, routeValues: routeValues); } + /// + /// Redirects to the specified route using the specified + /// and . + /// + /// The name of the route. + /// The parameters for a route. + /// The created for the response. [NonAction] - public RedirectToRouteResult RedirectToRoute(string routeName, object routeValues) + public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues) { return new RedirectToRouteResult(Url, routeName, routeValues); } + /// + /// Redirects to the specified route with set to true + /// using the specified . + /// + /// The name of the route. + /// The created for the response. [NonAction] - public RedirectToRouteResult RedirectToRoutePermanent(string routeName) + public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName) { return RedirectToRoutePermanent(routeName, routeValues: null); } + /// + /// Redirects to the specified route with set to true + /// using the specified . + /// + /// The parameters for a route. + /// The created for the response. [NonAction] - public RedirectToRouteResult RedirectToRoutePermanent(object routeValues) + public virtual RedirectToRouteResult RedirectToRoutePermanent(object routeValues) { return RedirectToRoutePermanent(routeName: null, routeValues: routeValues); } + /// + /// Redirects to the specified route with set to true + /// using the specified and . + /// + /// The name of the route. + /// The parameters for a route. + /// The created for the response. [NonAction] - public RedirectToRouteResult RedirectToRoutePermanent(string routeName, object routeValues) + public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, object routeValues) { return new RedirectToRouteResult(Url, routeName, routeValues, permanent: true); } + /// + /// Called before the action method is invoked. + /// + /// The action executing context. [NonAction] public virtual void OnActionExecuting([NotNull] ActionExecutingContext context) { } + /// + /// Called after the action method is invoked. + /// + /// The action executed context. [NonAction] public virtual void OnActionExecuted([NotNull] ActionExecutedContext context) { } + /// + /// Called before the action method is invoked. + /// + /// The action executing context. + /// The to execute. Invoke this delegate in the body + /// of to continue execution of the action. + /// A instance. [NonAction] public virtual async Task OnActionExecutionAsync( [NotNull] ActionExecutingContext context, diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/RedirectResultTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/RedirectResultTest.cs index 70a2d90eef..c0996575a4 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/RedirectResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/RedirectResultTest.cs @@ -14,6 +14,34 @@ namespace Microsoft.AspNet.Mvc.Core.Test { public class RedirectResultTest { + [Fact] + public void RedirectResult_Constructor_WithParameterUrl_SetsResultUrlAndNotPermanent() + { + // Arrange + var url = "/test/url"; + + // Act + var result = new RedirectResult(url); + + // Assert + Assert.False(result.Permanent); + Assert.Same(url, result.Url); + } + + [Fact] + public void RedirectResult_Constructor_WithParameterUrlAndPermanent_SetsResultUrlAndPermanent() + { + // Arrange + var url = "/test/url"; + + // Act + var result = new RedirectResult(url, permanent: true); + + // Assert + Assert.True(result.Permanent); + Assert.Same(url, result.Url); + } + [Theory] [InlineData("", "/Home/About", "/Home/About")] [InlineData("/myapproot", "/test", "/test")] diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs index 1c8fbc8b71..c7c24f2a64 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs @@ -52,37 +52,41 @@ namespace Microsoft.AspNet.Mvc.Test } [Fact] - public void Redirect_Temporary_SetsSameUrl() + public void Redirect_WithParameterUrl_SetsRedirectResultSameUrl() { // Arrange var controller = new Controller(); + var url = "/test/url"; // Act - var result = controller.Redirect("sample\\url"); + var result = controller.Redirect(url); // Assert + Assert.IsType(result); Assert.False(result.Permanent); - Assert.Equal("sample\\url", result.Url); + Assert.Same(url, result.Url); } [Fact] - public void Redirect_Permanent_SetsSameUrl() + public void RedirectPermanent_WithParameterUrl_SetsRedirectResultPermanentAndSameUrl() { // Arrange var controller = new Controller(); + var url = "/test/url"; // Act - var result = controller.RedirectPermanent("sample\\url"); + var result = controller.RedirectPermanent(url); // Assert + Assert.IsType(result); Assert.True(result.Permanent); - Assert.Equal("sample\\url", result.Url); + Assert.Same(url, result.Url); } [Theory] [InlineData(null)] [InlineData("")] - public void Redirect_NullOrEmptyUrl_Throws(string url) + public void Redirect_WithParameter_NullOrEmptyUrl_Throws(string url) { // Arrange var controller = new Controller(); @@ -95,7 +99,7 @@ namespace Microsoft.AspNet.Mvc.Test [Theory] [InlineData(null)] [InlineData("")] - public void RedirectPermanent_NullOrEmptyUrl_Throws(string url) + public void RedirectPermanent_WithParameter_NullOrEmptyUrl_Throws(string url) { // Arrange var controller = new Controller(); @@ -106,7 +110,7 @@ namespace Microsoft.AspNet.Mvc.Test } [Fact] - public void RedirectToAction_Temporary_Returns_SameAction() + public void RedirectToAction_WithParameterActionName_SetsResultActionName() { // Arrange var controller = new Controller(); @@ -115,12 +119,13 @@ namespace Microsoft.AspNet.Mvc.Test var resultTemporary = controller.RedirectToAction("SampleAction"); // Assert + Assert.IsType(resultTemporary); Assert.False(resultTemporary.Permanent); Assert.Equal("SampleAction", resultTemporary.ActionName); } [Fact] - public void RedirectToAction_Permanent_Returns_SameAction() + public void RedirectToActionPermanent_WithParameterActionName_SetsResultActionNameAndPermanent() { // Arrange var controller = new Controller(); @@ -129,6 +134,7 @@ namespace Microsoft.AspNet.Mvc.Test var resultPermanent = controller.RedirectToActionPermanent("SampleAction"); // Assert + Assert.IsType(resultPermanent); Assert.True(resultPermanent.Permanent); Assert.Equal("SampleAction", resultPermanent.ActionName); } @@ -137,7 +143,7 @@ namespace Microsoft.AspNet.Mvc.Test [InlineData("")] [InlineData(null)] [InlineData("SampleController")] - public void RedirectToAction_Temporary_Returns_SameController(string controllerName) + public void RedirectToAction_WithParameterActionAndControllerName_SetsEqualNames(string controllerName) { // Arrange var controller = new Controller(); @@ -146,6 +152,7 @@ namespace Microsoft.AspNet.Mvc.Test var resultTemporary = controller.RedirectToAction("SampleAction", controllerName); // Assert + Assert.IsType(resultTemporary); Assert.False(resultTemporary.Permanent); Assert.Equal("SampleAction", resultTemporary.ActionName); Assert.Equal(controllerName, resultTemporary.ControllerName); @@ -155,7 +162,8 @@ namespace Microsoft.AspNet.Mvc.Test [InlineData("")] [InlineData(null)] [InlineData("SampleController")] - public void RedirectToAction_Permanent_Returns_SameController(string controllerName) + public void RedirectToActionPermanent_WithParameterActionAndControllerName_SetsEqualNames( + string controllerName) { // Arrange var controller = new Controller(); @@ -164,6 +172,7 @@ namespace Microsoft.AspNet.Mvc.Test var resultPermanent = controller.RedirectToActionPermanent("SampleAction", controllerName); // Assert + Assert.IsType(resultPermanent); Assert.True(resultPermanent.Permanent); Assert.Equal("SampleAction", resultPermanent.ActionName); Assert.Equal(controllerName, resultPermanent.ControllerName); @@ -171,7 +180,7 @@ namespace Microsoft.AspNet.Mvc.Test [Theory] [MemberData("RedirectTestData")] - public void RedirectToAction_Temporary_Returns_SameActionControllerAndRouteValues(object routeValues) + public void RedirectToAction_WithParameterActionControllerRouteValues_SetsResultProperties(object routeValues) { // Arrange var controller = new Controller(); @@ -180,6 +189,7 @@ namespace Microsoft.AspNet.Mvc.Test var resultTemporary = controller.RedirectToAction("SampleAction", "SampleController", routeValues); // Assert + Assert.IsType(resultTemporary); Assert.False(resultTemporary.Permanent); Assert.Equal("SampleAction", resultTemporary.ActionName); Assert.Equal("SampleController", resultTemporary.ControllerName); @@ -188,15 +198,20 @@ namespace Microsoft.AspNet.Mvc.Test [Theory] [MemberData("RedirectTestData")] - public void RedirectToAction_Permanent_Returns_SameActionControllerAndRouteValues(object routeValues) + public void RedirectToActionPermanent_WithParameterActionControllerRouteValues_SetsResultProperties( + object routeValues) { // Arrange var controller = new Controller(); // Act - var resultPermanent = controller.RedirectToActionPermanent("SampleAction", "SampleController", routeValues); + var resultPermanent = controller.RedirectToActionPermanent( + "SampleAction", + "SampleController", + routeValues); // Assert + Assert.IsType(resultPermanent); Assert.True(resultPermanent.Permanent); Assert.Equal("SampleAction", resultPermanent.ActionName); Assert.Equal("SampleController", resultPermanent.ControllerName); @@ -205,7 +220,7 @@ namespace Microsoft.AspNet.Mvc.Test [Theory] [MemberData("RedirectTestData")] - public void RedirectToAction_Temporary_Returns_SameActionAndRouteValues(object routeValues) + public void RedirectToAction_WithParameterActionAndRouteValues_SetsResultProperties(object routeValues) { // Arrange var controller = new Controller(); @@ -214,6 +229,7 @@ namespace Microsoft.AspNet.Mvc.Test var resultTemporary = controller.RedirectToAction(actionName: null, routeValues: routeValues); // Assert + Assert.IsType(resultTemporary); Assert.False(resultTemporary.Permanent); Assert.Null(resultTemporary.ActionName); Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultTemporary.RouteValues); @@ -221,7 +237,8 @@ namespace Microsoft.AspNet.Mvc.Test [Theory] [MemberData("RedirectTestData")] - public void RedirectToAction_Permanent_Returns_SameActionAndRouteValues(object routeValues) + public void RedirectToActionPermanent_WithParameterActionAndRouteValues_SetsResultProperties( + object routeValues) { // Arrange var controller = new Controller(); @@ -230,6 +247,7 @@ namespace Microsoft.AspNet.Mvc.Test var resultPermanent = controller.RedirectToActionPermanent(null, routeValues); // Assert + Assert.IsType(resultPermanent); Assert.True(resultPermanent.Permanent); Assert.Null(resultPermanent.ActionName); Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultPermanent.RouteValues); @@ -237,7 +255,7 @@ namespace Microsoft.AspNet.Mvc.Test [Theory] [MemberData("RedirectTestData")] - public void RedirectToRoute_Temporary_Returns_SameRouteValues(object routeValues) + public void RedirectToRoute_WithParameterRouteValues_SetsResultEqualRouteValues(object routeValues) { // Arrange var controller = new Controller(); @@ -246,13 +264,15 @@ namespace Microsoft.AspNet.Mvc.Test var resultTemporary = controller.RedirectToRoute(routeValues); // Assert + Assert.IsType(resultTemporary); Assert.False(resultTemporary.Permanent); Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultTemporary.RouteValues); } [Theory] [MemberData("RedirectTestData")] - public void RedirectToRoute_Permanent_Returns_SameRouteValues(object routeValues) + public void RedirectToRoutePermanent_WithParameterRouteValues_SetsResultEqualRouteValuesAndPermanent( + object routeValues) { // Arrange var controller = new Controller(); @@ -261,10 +281,81 @@ namespace Microsoft.AspNet.Mvc.Test var resultPermanent = controller.RedirectToRoutePermanent(routeValues); // Assert + Assert.IsType(resultPermanent); Assert.True(resultPermanent.Permanent); Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultPermanent.RouteValues); } + [Fact] + public void RedirectToRoute_WithParameterRouteName_SetsResultSameRouteName() + { + // Arrange + var controller = new Controller(); + var routeName = "CustomRouteName"; + + // Act + var resultTemporary = controller.RedirectToRoute(routeName); + + // Assert + Assert.IsType(resultTemporary); + Assert.False(resultTemporary.Permanent); + Assert.Same(routeName, resultTemporary.RouteName); + } + + [Fact] + public void RedirectToRoutePermanent_WithParameterRouteName_SetsResultSameRouteNameAndPermanent() + { + // Arrange + var controller = new Controller(); + var routeName = "CustomRouteName"; + + // Act + var resultPermanent = controller.RedirectToRoutePermanent(routeName); + + // Assert + Assert.IsType(resultPermanent); + Assert.True(resultPermanent.Permanent); + Assert.Same(routeName, resultPermanent.RouteName); + } + + [Theory] + [MemberData("RedirectTestData")] + public void RedirectToRoute_WithParameterRouteNameAndRouteValues_SetsResultSameRouteNameAndRouteValues( + object routeValues) + { + // Arrange + var controller = new Controller(); + var routeName = "CustomRouteName"; + + // Act + var resultTemporary = controller.RedirectToRoute(routeName, routeValues); + + // Assert + Assert.IsType(resultTemporary); + Assert.False(resultTemporary.Permanent); + Assert.Same(routeName, resultTemporary.RouteName); + Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultTemporary.RouteValues); + } + + [Theory] + [MemberData("RedirectTestData")] + public void RedirectToRoutePermanent_WithParameterRouteNameAndRouteValues_SetsResultProperties( + object routeValues) + { + // Arrange + var controller = new Controller(); + var routeName = "CustomRouteName"; + + // Act + var resultPermanent = controller.RedirectToRoutePermanent(routeName, routeValues); + + // Assert + Assert.IsType(resultPermanent); + Assert.True(resultPermanent.Permanent); + Assert.Same(routeName, resultPermanent.RouteName); + Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultPermanent.RouteValues); + } + [Theory] [MemberData("PublicNormalMethodsFromController")] public void NonActionAttribute_IsOnEveryPublicNormalMethodFromController(MethodInfo method) @@ -419,14 +510,17 @@ namespace Microsoft.AspNet.Mvc.Test get { yield return new object[] { null }; - yield return - new object[] { - new Dictionary() { { "hello", "world" } } + yield return new object[] + { + new Dictionary() { { "hello", "world" } }, }; - yield return - new object[] { - new RouteValueDictionary(new Dictionary() { - { "test", "case" }, { "sample", "route" } }) + yield return new object[] + { + new RouteValueDictionary(new Dictionary() + { + { "test", "case" }, + { "sample", "route" }, + }), }; } }