diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ControllerBase.cs b/src/Microsoft.AspNetCore.Mvc.Core/ControllerBase.cs index 69050ee7d9..3fc8c444e4 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ControllerBase.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ControllerBase.cs @@ -1750,7 +1750,7 @@ namespace Microsoft.AspNetCore.Mvc /// /// Creates an that produces a response. /// - /// The model state dictionary containing errors to be returned to the client. + /// The containing errors to be returned to the client. /// The created for the response. [NonAction] public virtual BadRequestObjectResult BadRequest(ModelStateDictionary modelState) @@ -1787,7 +1787,7 @@ namespace Microsoft.AspNetCore.Mvc /// /// Creates an that produces a response. /// - /// The model state dictionary containing errors to be returned to the client. + /// The containing errors to be returned to the client. /// The created for the response. [NonAction] public virtual UnprocessableEntityObjectResult UnprocessableEntity(ModelStateDictionary modelState) @@ -1820,7 +1820,7 @@ namespace Microsoft.AspNetCore.Mvc /// /// Creates an that produces a response. /// - /// The model state dictionary containing errors to be returned to the client. + /// The containing errors to be returned to the client. /// The created for the response. [NonAction] public virtual ConflictObjectResult Conflict(ModelStateDictionary modelState) diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageBase.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageBase.cs index 5c7177183b..12b88777e8 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageBase.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageBase.cs @@ -145,6 +145,39 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages } } + /// + /// Creates a that produces a response. + /// + /// The created for the response. + [NonAction] + public virtual BadRequestResult BadRequest() + => new BadRequestResult(); + + /// + /// Creates a that produces a response. + /// + /// An error object to be returned to the client. + /// The created for the response. + [NonAction] + public virtual BadRequestObjectResult BadRequest(object error) + => new BadRequestObjectResult(error); + + /// + /// Creates a that produces a response. + /// + /// The containing errors to be returned to the client. + /// The created for the response. + [NonAction] + public virtual BadRequestObjectResult BadRequest(ModelStateDictionary modelState) + { + if (modelState == null) + { + throw new ArgumentNullException(nameof(modelState)); + } + + return new BadRequestObjectResult(modelState); + } + /// /// Creates a . /// diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs index b1f39ab164..ba9062bfe3 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs @@ -520,6 +520,39 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages propertyFilter); } + /// + /// Creates a that produces a response. + /// + /// The created for the response. + [NonAction] + public virtual BadRequestResult BadRequest() + => new BadRequestResult(); + + /// + /// Creates a that produces a response. + /// + /// An error object to be returned to the client. + /// The created for the response. + [NonAction] + public virtual BadRequestObjectResult BadRequest(object error) + => new BadRequestObjectResult(error); + + /// + /// Creates a that produces a response. + /// + /// The containing errors to be returned to the client. + /// The created for the response. + [NonAction] + public virtual BadRequestObjectResult BadRequest(ModelStateDictionary modelState) + { + if (modelState == null) + { + throw new ArgumentNullException(nameof(modelState)); + } + + return new BadRequestObjectResult(modelState); + } + /// /// Creates a . /// diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs index 4731e2a323..85be6aa07d 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs @@ -291,6 +291,20 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); } + [Fact] + public async Task PageHandlerCanReturnBadRequest() + { + // Arrange + var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Pages/HandlerWithParameter"); + + // Act + var response = await Client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + Assert.Equal("Parameter cannot be null.", await response.Content.ReadAsStringAsync()); + } + [Fact] public async Task HelloWorld_CanGetContent() { diff --git a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageModelTest.cs b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageModelTest.cs index 814504618b..11391095e5 100644 --- a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageModelTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageModelTest.cs @@ -1495,6 +1495,35 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages Assert.Equal(StatusCodes.Status401Unauthorized, result.StatusCode); } + [Fact] + public void BadRequest_SetsStatusCode() + { + // Arrange + var page = new TestPage(); + + // Act + var result = page.BadRequest(); + + // Assert + Assert.IsType(result); + Assert.Equal(StatusCodes.Status400BadRequest, result.StatusCode); + } + + [Fact] + public void BadRequest_SetsStatusCodeAndResponseContent() + { + // Arrange + var page = new TestPage(); + + // Act + var result = page.BadRequest("Test Content"); + + // Assert + Assert.IsType(result); + Assert.Equal(StatusCodes.Status400BadRequest, result.StatusCode); + Assert.Equal("Test Content", result.Value); + } + [Fact] public void NotFound_SetsStatusCode() { diff --git a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageTest.cs b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageTest.cs index 661130c1c6..aaefccfcc9 100644 --- a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageTest.cs @@ -1486,6 +1486,35 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages Assert.Equal(StatusCodes.Status401Unauthorized, result.StatusCode); } + [Fact] + public void BadRequest_SetsStatusCode() + { + // Arrange + var page = new TestPage(); + + // Act + var result = page.BadRequest(); + + // Assert + Assert.IsType(result); + Assert.Equal(StatusCodes.Status400BadRequest, result.StatusCode); + } + + [Fact] + public void BadRequest_SetsStatusCodeAndResponseContent() + { + // Arrange + var page = new TestPage(); + + // Act + var result = page.BadRequest("Test Content"); + + // Assert + Assert.IsType(result); + Assert.Equal(StatusCodes.Status400BadRequest, result.StatusCode); + Assert.Equal("Test Content", result.Value); + } + [Fact] public void NotFound_SetsStatusCode() { diff --git a/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelAttributeModel.cs b/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelAttributeModel.cs index 4a81b6cac0..fb924ba5ec 100644 --- a/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelAttributeModel.cs +++ b/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelAttributeModel.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; namespace RazorPagesWebSite diff --git a/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cs b/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cs index ec8dc27137..d6622314da 100644 --- a/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cs +++ b/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc.RazorPages; namespace RazorPagesWebSite diff --git a/test/WebSites/RazorPagesWebSite/ModelWithPageFilter.cs b/test/WebSites/RazorPagesWebSite/ModelWithPageFilter.cs index 1d2d85c3eb..03d2f9a8f4 100644 --- a/test/WebSites/RazorPagesWebSite/ModelWithPageFilter.cs +++ b/test/WebSites/RazorPagesWebSite/ModelWithPageFilter.cs @@ -3,7 +3,6 @@ using System; using System.Linq; -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.RazorPages; diff --git a/test/WebSites/RazorPagesWebSite/Pages/HandlerWithParameter.cshtml b/test/WebSites/RazorPagesWebSite/Pages/HandlerWithParameter.cshtml new file mode 100644 index 0000000000..e90d3cf674 --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/Pages/HandlerWithParameter.cshtml @@ -0,0 +1,5 @@ +@page +@model HandlerWithParameterModel +@{ +} +Handler with parameter diff --git a/test/WebSites/RazorPagesWebSite/Pages/HandlerWithParameter.cshtml.cs b/test/WebSites/RazorPagesWebSite/Pages/HandlerWithParameter.cshtml.cs new file mode 100644 index 0000000000..d1b8ade28a --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/Pages/HandlerWithParameter.cshtml.cs @@ -0,0 +1,21 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace RazorPagesWebSite.Pages +{ + public class HandlerWithParameterModel : PageModel + { + public IActionResult OnGet(string testParameter = null) + { + if (testParameter == null) + { + return BadRequest("Parameter cannot be null."); + } + + return Page(); + } + } +}