From b1b3a816cc808037d3e6bb45c955b344792a9343 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 26 Apr 2017 18:06:34 -0700 Subject: [PATCH] Rename View() to Page() This requires us to introduce another base class between RazorPageBase and Page - you need this because you aren't allow to have Page.Page(). --- samples/MvcSandbox/Models/TestModel.cs | 2 +- .../Infrastructure/PageResultExecutor.cs | 2 +- .../Internal/PageActionInvoker.cs | 2 +- .../Page.cs | 1551 +--------------- .../PageBase.cs | 1580 +++++++++++++++++ .../PageModel.cs | 20 +- .../{PageViewResult.cs => PageResult.cs} | 16 +- .../Internal/PageActionInvokerTest.cs | 2 +- .../PageModelTest.cs | 5 +- .../RazorPagesWebSite/HandlerTestPage.cshtml | 4 +- .../ModelHandlerTestModel.cs | 4 +- .../RazorPagesWebSite/OnGetView.cshtml | 2 +- .../Pages/TagHelper/PostWithHandler.cshtml | 4 +- .../TempData/TempDataPageModel.cs | 4 +- 14 files changed, 1610 insertions(+), 1588 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Mvc.RazorPages/PageBase.cs rename src/Microsoft.AspNetCore.Mvc.RazorPages/{PageViewResult.cs => PageResult.cs} (77%) diff --git a/samples/MvcSandbox/Models/TestModel.cs b/samples/MvcSandbox/Models/TestModel.cs index a3233fd0e8..29e881e9a4 100644 --- a/samples/MvcSandbox/Models/TestModel.cs +++ b/samples/MvcSandbox/Models/TestModel.cs @@ -13,7 +13,7 @@ namespace MvcSandbox public IActionResult OnPost(string name) { Name = name; - return View(); + return Page(); } } } diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageResultExecutor.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageResultExecutor.cs index 648841f361..39ef13ceeb 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageResultExecutor.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageResultExecutor.cs @@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure /// /// Executes a Razor Page asynchronously. /// - public virtual Task ExecuteAsync(PageContext pageContext, PageViewResult result) + public virtual Task ExecuteAsync(PageContext pageContext, PageResult result) { if (result.Model != null) { diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PageActionInvoker.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PageActionInvoker.cs index d15507be68..272a49d5a3 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PageActionInvoker.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PageActionInvoker.cs @@ -394,7 +394,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal if (result == null) { - result = new PageViewResult(_page); + result = new PageResult(_page); } await result.ExecuteResultAsync(_pageContext); diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Page.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Page.cs index f7c25b428b..daf6036aa1 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Page.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Page.cs @@ -26,1556 +26,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages /// A base class for a Razor page. /// [PagesBaseClass] - public abstract class Page : RazorPageBase, IRazorPage + public abstract class Page : PageBase { - private IObjectModelValidator _objectValidator; - private IModelMetadataProvider _metadataProvider; - private IModelBinderFactory _modelBinderFactory; - - /// - /// The . - /// - public PageContext PageContext { get; set; } - - /// - public override ViewContext ViewContext - { - get => PageContext; - set - { - PageContext = (PageContext)value; - } - } - - /// - /// Gets the . - /// - public HttpContext HttpContext => PageContext?.HttpContext; - - /// - /// Gets the . - /// - public HttpRequest Request => HttpContext?.Request; - - /// - /// Gets the . - /// - public HttpResponse Response => HttpContext?.Response; - - /// - /// Gets the for the executing action. - /// - public RouteData RouteData - { - get - { - return PageContext.RouteData; - } - } - - /// - /// Gets the . - /// - public ModelStateDictionary ModelState => PageContext?.ModelState; - - private IObjectModelValidator ObjectValidator - { - get - { - if (_objectValidator == null) - { - _objectValidator = HttpContext?.RequestServices?.GetRequiredService(); - } - - return _objectValidator; - } - } - - private IModelMetadataProvider MetadataProvider - { - get - { - if (_metadataProvider == null) - { - _metadataProvider = HttpContext?.RequestServices?.GetRequiredService(); - } - - return _metadataProvider; - } - } - - private IModelBinderFactory ModelBinderFactory - { - get - { - if (_modelBinderFactory == null) - { - _modelBinderFactory = HttpContext?.RequestServices?.GetRequiredService(); - } - - return _modelBinderFactory; - } - } - - /// - public override void EnsureRenderedBodyOrSections() - { - throw new NotSupportedException(); - } - - /// - public override void BeginContext(int position, int length, bool isLiteral) - { - const string BeginContextEvent = "Microsoft.AspNetCore.Mvc.Razor.BeginInstrumentationContext"; - - if (DiagnosticSource?.IsEnabled(BeginContextEvent) == true) - { - DiagnosticSource.Write( - BeginContextEvent, - new - { - httpContext = ViewContext, - path = Path, - position = position, - length = length, - isLiteral = isLiteral, - }); - } - } - - /// - public override void EndContext() - { - const string EndContextEvent = "Microsoft.AspNetCore.Mvc.Razor.EndInstrumentationContext"; - - if (DiagnosticSource?.IsEnabled(EndContextEvent) == true) - { - DiagnosticSource.Write( - EndContextEvent, - new - { - httpContext = ViewContext, - path = Path, - }); - } - } - - /// - /// Creates a . - /// - /// The created for the response. - /// - /// The behavior of this method depends on the in use. - /// and - /// are among likely status results. - /// - public virtual ChallengeResult Challenge() - => new ChallengeResult(); - - /// - /// Creates a with the specified authentication schemes. - /// - /// The authentication schemes to challenge. - /// The created for the response. - /// - /// The behavior of this method depends on the in use. - /// and - /// are among likely status results. - /// - public virtual ChallengeResult Challenge(params string[] authenticationSchemes) - => new ChallengeResult(authenticationSchemes); - - /// - /// Creates a with the specified . - /// - /// used to perform the authentication - /// challenge. - /// The created for the response. - /// - /// The behavior of this method depends on the in use. - /// and - /// are among likely status results. - /// - public virtual ChallengeResult Challenge(AuthenticationProperties properties) - => new ChallengeResult(properties); - - /// - /// Creates a with the specified specified authentication schemes and - /// . - /// - /// used to perform the authentication - /// challenge. - /// The authentication schemes to challenge. - /// The created for the response. - /// - /// The behavior of this method depends on the in use. - /// and - /// are among likely status results. - /// - public virtual ChallengeResult Challenge( - AuthenticationProperties properties, - params string[] authenticationSchemes) - => new ChallengeResult(authenticationSchemes, properties); - - /// - /// Creates a object with by specifying a - /// string. - /// - /// The content to write to the response. - /// The created object for the response. - public virtual ContentResult Content(string content) - => Content(content, (MediaTypeHeaderValue)null); - - /// - /// Creates a object with 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. - public virtual ContentResult Content(string content, string contentType) - => Content(content, MediaTypeHeaderValue.Parse(contentType)); - - /// - /// Creates a object with 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. - /// - /// If encoding is provided by both the 'charset' and the parameters, then - /// the parameter is chosen as the final encoding. - /// - public virtual ContentResult Content(string content, string contentType, Encoding contentEncoding) - { - var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(contentType); - mediaTypeHeaderValue.Encoding = contentEncoding ?? mediaTypeHeaderValue.Encoding; - return Content(content, mediaTypeHeaderValue); - } - - /// - /// Creates a object with by specifying a - /// string and a . - /// - /// The content to write to the response. - /// The content type (MIME type). - /// The created object for the response. - public virtual ContentResult Content(string content, MediaTypeHeaderValue contentType) - { - return new ContentResult - { - Content = content, - ContentType = contentType?.ToString() - }; - } - - /// - /// Creates a ( by default). - /// - /// The created for the response. - /// - /// Some authentication schemes, such as cookies, will convert to - /// a redirect to show a login page. - /// - public virtual ForbidResult Forbid() - => new ForbidResult(); - - /// - /// Creates a ( by default) with the - /// specified authentication schemes. - /// - /// The authentication schemes to challenge. - /// The created for the response. - /// - /// Some authentication schemes, such as cookies, will convert to - /// a redirect to show a login page. - /// - public virtual ForbidResult Forbid(params string[] authenticationSchemes) - => new ForbidResult(authenticationSchemes); - - /// - /// Creates a ( by default) with the - /// specified . - /// - /// used to perform the authentication - /// challenge. - /// The created for the response. - /// - /// Some authentication schemes, such as cookies, will convert to - /// a redirect to show a login page. - /// - public virtual ForbidResult Forbid(AuthenticationProperties properties) - => new ForbidResult(properties); - - /// - /// Creates a ( by default) with the - /// specified specified authentication schemes and . - /// - /// used to perform the authentication - /// challenge. - /// The authentication schemes to challenge. - /// The created for the response. - /// - /// Some authentication schemes, such as cookies, will convert to - /// a redirect to show a login page. - /// - public virtual ForbidResult Forbid(AuthenticationProperties properties, params string[] authenticationSchemes) - => new ForbidResult(authenticationSchemes, properties); - - /// - /// Returns a file with the specified as content - /// () and the specified as the Content-Type. - /// - /// The file contents. - /// The Content-Type of the file. - /// The created for the response. - public virtual FileContentResult File(byte[] fileContents, string contentType) - => File(fileContents, contentType, fileDownloadName: null); - - /// - /// Returns a file with the specified as content (), the - /// specified as the Content-Type and the - /// specified as the suggested file name. - /// - /// The file contents. - /// The Content-Type of the file. - /// The suggested file name. - /// The created for the response. - public virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName) - => new FileContentResult(fileContents, contentType) { FileDownloadName = fileDownloadName }; - - /// - /// Returns a file in the specified () - /// with the specified as the Content-Type. - /// - /// The with the contents of the file. - /// The Content-Type of the file. - /// The created for the response. - public virtual FileStreamResult File(Stream fileStream, string contentType) - => File(fileStream, contentType, fileDownloadName: null); - - /// - /// Returns a file in the specified () with the - /// specified as the Content-Type and the - /// specified as the suggested file name. - /// - /// The with the contents of the file. - /// The Content-Type of the file. - /// The suggested file name. - /// The created for the response. - public virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName) - => new FileStreamResult(fileStream, contentType) { FileDownloadName = fileDownloadName }; - - /// - /// Returns the file specified by () with the - /// specified as the Content-Type. - /// - /// The virtual path of the file to be returned. - /// The Content-Type of the file. - /// The created for the response. - public virtual VirtualFileResult File(string virtualPath, string contentType) - => File(virtualPath, contentType, fileDownloadName: null); - - /// - /// Returns the file specified by () with the - /// specified as the Content-Type and the - /// specified as the suggested file name. - /// - /// The virtual path of the file to be returned. - /// The Content-Type of the file. - /// The suggested file name. - /// The created for the response. - public virtual VirtualFileResult File(string virtualPath, string contentType, string fileDownloadName) - => new VirtualFileResult(virtualPath, contentType) { FileDownloadName = fileDownloadName }; - - /// - /// Returns the file specified by () with the - /// specified as the Content-Type. - /// - /// The physical path of the file to be returned. - /// The Content-Type of the file. - /// The created for the response. - public virtual PhysicalFileResult PhysicalFile(string physicalPath, string contentType) - => PhysicalFile(physicalPath, contentType, fileDownloadName: null); - - /// - /// Returns the file specified by () with the - /// specified as the Content-Type and the - /// specified as the suggested file name. - /// - /// The physical path of the file to be returned. - /// The Content-Type of the file. - /// The suggested file name. - /// The created for the response. - public virtual PhysicalFileResult PhysicalFile( - string physicalPath, - string contentType, - string fileDownloadName) - => new PhysicalFileResult(physicalPath, contentType) { FileDownloadName = fileDownloadName }; - - /// - /// Creates a object that redirects - /// () to the specified local . - /// - /// The local URL to redirect to. - /// The created for the response. - public virtual LocalRedirectResult LocalRedirect(string localUrl) - { - if (string.IsNullOrEmpty(localUrl)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(localUrl)); - } - - return new LocalRedirectResult(localUrl); - } - - /// - /// Creates a object with set to - /// true () using the specified . - /// - /// The local URL to redirect to. - /// The created for the response. - public virtual LocalRedirectResult LocalRedirectPermanent(string localUrl) - { - if (string.IsNullOrEmpty(localUrl)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(localUrl)); - } - - return new LocalRedirectResult(localUrl, permanent: true); - } - - /// - /// Creates a object with set to - /// false and set to true - /// () using the specified . - /// - /// The local URL to redirect to. - /// The created for the response. - public virtual LocalRedirectResult LocalRedirectPreserveMethod(string localUrl) - { - if (string.IsNullOrEmpty(localUrl)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(localUrl)); - } - - return new LocalRedirectResult(localUrl: localUrl, permanent: false, preserveMethod: true); - } - - /// - /// Creates a object with set to - /// true and set to true - /// () using the specified . - /// - /// The local URL to redirect to. - /// The created for the response. - public virtual LocalRedirectResult LocalRedirectPermanentPreserveMethod(string localUrl) - { - if (string.IsNullOrEmpty(localUrl)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(localUrl)); - } - - return new LocalRedirectResult(localUrl: localUrl, permanent: true, preserveMethod: true); - } - - /// - /// Creates an that produces a response. - /// - /// The created for the response. - public virtual NotFoundResult NotFound() - => new NotFoundResult(); - - /// - /// Creates an that produces a response. - /// - /// The created for the response. - public virtual NotFoundObjectResult NotFound(object value) - => new NotFoundObjectResult(value); - - /// - /// Creates a object that redirects to the specified . - /// - /// The URL to redirect to. - /// The created for the response. - public virtual RedirectResult Redirect(string url) - { - if (string.IsNullOrEmpty(url)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url)); - } - - return new RedirectResult(url); - } - - /// - /// Creates a object with set to true - /// () using the specified . - /// - /// The URL to redirect to. - /// The created for the response. - public virtual RedirectResult RedirectPermanent(string url) - { - if (string.IsNullOrEmpty(url)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url)); - } - - return new RedirectResult(url, permanent: true); - } - - /// - /// Creates a object with set to false - /// and set to true () - /// using the specified . - /// - /// The URL to redirect to. - /// The created for the response. - public virtual RedirectResult RedirectPreserveMethod(string url) - { - if (string.IsNullOrEmpty(url)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url)); - } - - return new RedirectResult(url: url, permanent: false, preserveMethod: true); - } - - /// - /// Creates a object with set to true - /// and set to true () - /// using the specified . - /// - /// The URL to redirect to. - /// The created for the response. - public virtual RedirectResult RedirectPermanentPreserveMethod(string url) - { - if (string.IsNullOrEmpty(url)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url)); - } - - return new RedirectResult(url: url, permanent: true, preserveMethod: true); - } - - /// - /// Redirects () to the specified action using the . - /// - /// The name of the action. - /// The created for the response. - public virtual RedirectToActionResult RedirectToAction(string actionName) - => 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. - public virtual RedirectToActionResult RedirectToAction(string actionName, object routeValues) - => 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. - public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName) - => 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. - public virtual RedirectToActionResult RedirectToAction( - string actionName, - string controllerName, - object routeValues) - => RedirectToAction(actionName, controllerName, routeValues, fragment: null); - - /// - /// Redirects () to the specified action using the specified - /// , , and . - /// - /// The name of the action. - /// The name of the controller. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToActionResult RedirectToAction( - string actionName, - string controllerName, - string fragment) - => RedirectToAction(actionName, controllerName, routeValues: null, fragment: fragment); - - /// - /// 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 fragment to add to the URL. - /// The created for the response. - public virtual RedirectToActionResult RedirectToAction( - string actionName, - string controllerName, - object routeValues, - string fragment) - => new RedirectToActionResult(actionName, controllerName, routeValues, fragment); - - /// - /// Redirects () to the specified action with - /// set to false and - /// set to true, using the specified , , - /// , and . - /// - /// The name of the action. - /// The name of the controller. - /// The route data to use for generating the URL. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToActionResult RedirectToActionPreserveMethod( - string actionName = null, - string controllerName = null, - object routeValues = null, - string fragment = null) - { - return new RedirectToActionResult( - actionName: actionName, - controllerName: controllerName, - routeValues: routeValues, - permanent: false, - preserveMethod: true, - fragment: fragment); - } - - /// - /// Redirects () to the specified action with - /// set to true using the specified . - /// - /// The name of the action. - /// The created for the response. - 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. - 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. - 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 fragment to add to the URL. - /// The created for the response. - public virtual RedirectToActionResult RedirectToActionPermanent( - string actionName, - string controllerName, - string fragment) - { - return RedirectToActionPermanent(actionName, controllerName, routeValues: null, fragment: fragment); - } - - /// - /// 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. - public virtual RedirectToActionResult RedirectToActionPermanent( - string actionName, - string controllerName, - object routeValues) - { - return RedirectToActionPermanent(actionName, controllerName, routeValues, fragment: 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 fragment to add to the URL. - /// The created for the response. - public virtual RedirectToActionResult RedirectToActionPermanent( - string actionName, - string controllerName, - object routeValues, - string fragment) - { - return new RedirectToActionResult( - actionName, - controllerName, - routeValues, - permanent: true, - fragment: fragment); - } - - /// - /// Redirects () to the specified action with - /// set to true and - /// set to true, using the specified , , - /// , and . - /// - /// The name of the action. - /// The name of the controller. - /// The route data to use for generating the URL. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToActionResult RedirectToActionPermanentPreserveMethod( - string actionName = null, - string controllerName = null, - object routeValues = null, - string fragment = null) - { - return new RedirectToActionResult( - actionName: actionName, - controllerName: controllerName, - routeValues: routeValues, - permanent: true, - preserveMethod: true, - fragment: fragment); - } - - /// - /// Redirects () to the specified route using the specified . - /// - /// The name of the route. - /// The created for the response. - 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. - 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. - public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues) - { - return RedirectToRoute(routeName, routeValues, fragment: null); - } - - /// - /// Redirects () to the specified route using the specified - /// and . - /// - /// The name of the route. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToRouteResult RedirectToRoute(string routeName, string fragment) - { - return RedirectToRoute(routeName, routeValues: null, fragment: fragment); - } - - /// - /// Redirects () to the specified route using the specified - /// , , and . - /// - /// The name of the route. - /// The parameters for a route. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToRouteResult RedirectToRoute( - string routeName, - object routeValues, - string fragment) - { - return new RedirectToRouteResult(routeName, routeValues, fragment); - } - - /// - /// Redirects () to the specified route with - /// set to false and - /// set to true, using the specified , , and . - /// - /// The name of the route. - /// The route data to use for generating the URL. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToRouteResult RedirectToRoutePreserveMethod( - string routeName = null, - object routeValues = null, - string fragment = null) - { - return new RedirectToRouteResult( - routeName: routeName, - routeValues: routeValues, - permanent: false, - preserveMethod: true, - fragment: fragment); - } - - /// - /// Redirects () to the specified route with - /// set to true using the specified . - /// - /// The name of the route. - /// The created for the response. - 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. - 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. - public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, object routeValues) - { - return RedirectToRoutePermanent(routeName, routeValues, fragment: null); - } - - /// - /// Redirects () to the specified route with - /// set to true using the specified - /// and . - /// - /// The name of the route. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, string fragment) - { - return RedirectToRoutePermanent(routeName, routeValues: null, fragment: fragment); - } - - /// - /// Redirects () to the specified route with - /// set to true using the specified , - /// , and . - /// - /// The name of the route. - /// The parameters for a route. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToRouteResult RedirectToRoutePermanent( - string routeName, - object routeValues, - string fragment) - => new RedirectToRouteResult(routeName, routeValues, permanent: true, fragment: fragment); - - /// - /// Redirects () to the specified route with - /// set to true and - /// set to true, using the specified , , and . - /// - /// The name of the route. - /// The route data to use for generating the URL. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToRouteResult RedirectToRoutePermanentPreserveMethod( - string routeName = null, - object routeValues = null, - string fragment = null) - { - return new RedirectToRouteResult( - routeName: routeName, - routeValues: routeValues, - permanent: true, - preserveMethod: true, - fragment: fragment); - } - - /// - /// Redirects () to the current page. - /// - /// The . - public virtual RedirectToPageResult RedirectToPage() - => RedirectToPage(pageName: null); - - /// - /// Redirects () to the current page with the specified . - /// - /// The parameters for a route. - /// The . - public virtual RedirectToPageResult RedirectToPage(object routeValues) - => RedirectToPage(pageName: null, routeValues: routeValues); - - /// - /// Redirects () to the specified . - /// - /// The name of the page. - /// The . - public virtual RedirectToPageResult RedirectToPage(string pageName) - => RedirectToPage(pageName, routeValues: null); - - /// - /// Redirects () to the specified - /// using the specified . - /// - /// The name of the page. - /// The page handler to redirect to. - /// The . - public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler) - => RedirectToPage(pageName, routeValues: null); - - /// - /// Redirects () to the specified - /// using the specified . - /// - /// The name of the page. - /// The parameters for a route. - /// The . - public virtual RedirectToPageResult RedirectToPage(string pageName, object routeValues) - => RedirectToPage(pageName, pageHandler: null, routeValues: routeValues, fragment: null); - - /// - /// Redirects () to the specified - /// using the specified . - /// - /// The name of the page. - /// The page handler to redirect to. - /// The fragment to add to the URL. - /// The . - public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler, string fragment) - => RedirectToPage(pageName, pageHandler, routeValues: null, fragment: fragment); - - /// - /// Redirects () to the specified - /// using the specified and . - /// - /// The name of the page. - /// The page handler to redirect to. - /// The parameters for a route. - /// The fragment to add to the URL. - /// The . - public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler, object routeValues, string fragment) - => new RedirectToPageResult(pageName, pageHandler, routeValues, fragment); - - /// - /// Redirects () to the specified . - /// - /// The name of the page. - /// The with set. - public virtual RedirectToPageResult RedirectToPagePermanent(string pageName) - => RedirectToPagePermanent(pageName, pageHandler: null); - - /// - /// Redirects () to the specified - /// using the specified . - /// - /// The name of the page. - /// The parameters for a route. - /// The with set. - public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, object routeValues) - => RedirectToPagePermanent(pageName, pageHandler: null, routeValues: routeValues, fragment: null); - - /// - /// Redirects () to the specified - /// using the specified . - /// - /// The name of the page. - /// The page handler to redirect to. - /// The with set. - public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler) - => RedirectToPagePermanent(pageName, pageHandler, routeValues: null); - - /// - /// Redirects () to the specified - /// using the specified . - /// - /// The name of the page. - /// The page handler to redirect to. - /// The parameters for a route. - /// The with set. - public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler, object routeValues) - => RedirectToPagePermanent(pageName, pageHandler, routeValues, fragment: null); - - /// - /// Redirects () to the specified - /// using the specified . - /// - /// The name of the page. - /// The page handler to redirect to. - /// The fragment to add to the URL. - /// The with set. - public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler, string fragment) - => RedirectToPagePermanent(pageName, pageHandler, routeValues: null, fragment: fragment); - - /// - /// Redirects () to the specified - /// using the specified and . - /// - /// The name of the page. - /// The page handler to redirect to. - /// The parameters for a route. - /// The fragment to add to the URL. - /// The with set. - protected RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler, object routeValues, string fragment) - => new RedirectToPageResult(pageName, pageHandler, routeValues, permanent: true, fragment: fragment); - - /// - /// Redirects () to the specified page with - /// set to false and - /// set to true, using the specified , , and . - /// - /// The name of the page. - /// The page handler to redirect to. - /// The route data to use for generating the URL. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToPageResult RedirectToPagePreserveMethod( - string pageName = null, - string pageHandler = null, - object routeValues = null, - string fragment = null) - { - return new RedirectToPageResult( - pageName: pageName, - pageHandler: pageHandler, - routeValues: routeValues, - permanent: false, - preserveMethod: true, - fragment: fragment); - } - - /// - /// Redirects () to the specified route with - /// set to true and - /// set to true, using the specified , , and . - /// - /// The name of the page. - /// The page handler to redirect to. - /// The route data to use for generating the URL. - /// The fragment to add to the URL. - /// The created for the response. - public virtual RedirectToPageResult RedirectToPagePermanentPreserveMethod( - string pageName = null, - string pageHandler = null, - object routeValues = null, - string fragment = null) - { - return new RedirectToPageResult( - pageName: pageName, - pageHandler: pageHandler, - routeValues: routeValues, - permanent: true, - preserveMethod: true, - fragment: fragment); - } - - /// - /// Creates a with the specified authentication scheme. - /// - /// The containing the user claims. - /// The authentication scheme to use for the sign-in operation. - /// The created for the response. - public virtual SignInResult SignIn(ClaimsPrincipal principal, string authenticationScheme) - => new SignInResult(authenticationScheme, principal); - - /// - /// Creates a with the specified specified authentication scheme and - /// . - /// - /// The containing the user claims. - /// used to perform the sign-in operation. - /// The authentication scheme to use for the sign-in operation. - /// The created for the response. - public virtual SignInResult SignIn( - ClaimsPrincipal principal, - AuthenticationProperties properties, - string authenticationScheme) - => new SignInResult(authenticationScheme, principal, properties); - - /// - /// Creates a with the specified authentication schemes. - /// - /// The authentication schemes to use for the sign-out operation. - /// The created for the response. - public virtual SignOutResult SignOut(params string[] authenticationSchemes) - => new SignOutResult(authenticationSchemes); - - /// - /// Creates a with the specified specified authentication schemes and - /// . - /// - /// used to perform the sign-out operation. - /// The authentication scheme to use for the sign-out operation. - /// The created for the response. - public virtual SignOutResult SignOut(AuthenticationProperties properties, params string[] authenticationSchemes) - => new SignOutResult(authenticationSchemes, properties); - - /// - /// Creates a object by specifying a . - /// - /// The status code to set on the response. - /// The created object for the response. - public virtual StatusCodeResult StatusCode(int statusCode) - => new StatusCodeResult(statusCode); - - /// - /// Creates a object by specifying a and - /// - /// The status code to set on the response. - /// The value to set on the . - /// The created object for the response. - public virtual ObjectResult StatusCode(int statusCode, object value) - => new ObjectResult(value) { StatusCode = statusCode }; - - /// - /// Creates an that produces an response. - /// - /// The created for the response. - public virtual UnauthorizedResult Unauthorized() - => new UnauthorizedResult(); - - /// - /// Creates a object that renders this page as a view to the response. - /// - /// The created object for the response. - /// - /// Returning a from a page handler method is equivalent to returning void. - /// The view associated with the page will be executed. - /// - protected PageViewResult View() => new PageViewResult(this); - - /// - /// Updates the specified instance using values from the 's current - /// . - /// - /// The type of the model object. - /// The model instance to update. - /// A that on completion returns true if the update is successful. - public virtual Task TryUpdateModelAsync( - TModel model) - where TModel : class - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - return TryUpdateModelAsync(model, prefix: string.Empty); - } - - /// - /// Updates the specified instance using values from the 's current - /// and a . - /// - /// The type of the model object. - /// The model instance to update. - /// The prefix to use when looking up values in the current . - /// - /// A that on completion returns true if the update is successful. - public virtual async Task TryUpdateModelAsync( - TModel model, - string prefix) - where TModel : class - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - if (prefix == null) - { - throw new ArgumentNullException(nameof(prefix)); - } - var valueProvider = await CompositeValueProvider.CreateAsync(PageContext, PageContext.ValueProviderFactories); - return await TryUpdateModelAsync(model, prefix, valueProvider); - } - - /// - /// Updates the specified instance using the and a - /// . - /// - /// The type of the model object. - /// The model instance to update. - /// The prefix to use when looking up values in the . - /// - /// The used for looking up values. - /// A that on completion returns true if the update is successful. - public virtual Task TryUpdateModelAsync( - TModel model, - string prefix, - IValueProvider valueProvider) - where TModel : class - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - if (prefix == null) - { - throw new ArgumentNullException(nameof(prefix)); - } - - if (valueProvider == null) - { - throw new ArgumentNullException(nameof(valueProvider)); - } - - return ModelBindingHelper.TryUpdateModelAsync( - model, - prefix, - PageContext, - MetadataProvider, - ModelBinderFactory, - valueProvider, - ObjectValidator); - } - - /// - /// Updates the specified instance using values from the 's current - /// and a . - /// - /// The type of the model object. - /// The model instance to update. - /// The prefix to use when looking up values in the current . - /// - /// (s) which represent top-level properties - /// which need to be included for the current model. - /// A that on completion returns true if the update is successful. - public async Task TryUpdateModelAsync( - TModel model, - string prefix, - params Expression>[] includeExpressions) - where TModel : class - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - if (includeExpressions == null) - { - throw new ArgumentNullException(nameof(includeExpressions)); - } - - var valueProvider = await CompositeValueProvider.CreateAsync(PageContext, PageContext.ValueProviderFactories); - return await ModelBindingHelper.TryUpdateModelAsync( - model, - prefix, - PageContext, - MetadataProvider, - ModelBinderFactory, - valueProvider, - ObjectValidator, - includeExpressions); - } - - /// - /// Updates the specified instance using values from the 's current - /// and a . - /// - /// The type of the model object. - /// The model instance to update. - /// The prefix to use when looking up values in the current . - /// - /// A predicate which can be used to filter properties at runtime. - /// A that on completion returns true if the update is successful. - public async Task TryUpdateModelAsync( - TModel model, - string prefix, - Func propertyFilter) - where TModel : class - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - if (propertyFilter == null) - { - throw new ArgumentNullException(nameof(propertyFilter)); - } - - var valueProvider = await CompositeValueProvider.CreateAsync(PageContext, PageContext.ValueProviderFactories); - return await ModelBindingHelper.TryUpdateModelAsync( - model, - prefix, - PageContext, - MetadataProvider, - ModelBinderFactory, - valueProvider, - ObjectValidator, - propertyFilter); - } - - /// - /// Updates the specified instance using the and a - /// . - /// - /// The type of the model object. - /// The model instance to update. - /// The prefix to use when looking up values in the . - /// - /// The used for looking up values. - /// (s) which represent top-level properties - /// which need to be included for the current model. - /// A that on completion returns true if the update is successful. - public Task TryUpdateModelAsync( - TModel model, - string prefix, - IValueProvider valueProvider, - params Expression>[] includeExpressions) - where TModel : class - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - if (valueProvider == null) - { - throw new ArgumentNullException(nameof(valueProvider)); - } - - if (includeExpressions == null) - { - throw new ArgumentNullException(nameof(includeExpressions)); - } - - return ModelBindingHelper.TryUpdateModelAsync( - model, - prefix, - PageContext, - MetadataProvider, - ModelBinderFactory, - valueProvider, - ObjectValidator, - includeExpressions); - } - - /// - /// Updates the specified instance using the and a - /// . - /// - /// The type of the model object. - /// The model instance to update. - /// The prefix to use when looking up values in the . - /// - /// The used for looking up values. - /// A predicate which can be used to filter properties at runtime. - /// A that on completion returns true if the update is successful. - public Task TryUpdateModelAsync( - TModel model, - string prefix, - IValueProvider valueProvider, - Func propertyFilter) - where TModel : class - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - if (valueProvider == null) - { - throw new ArgumentNullException(nameof(valueProvider)); - } - - if (propertyFilter == null) - { - throw new ArgumentNullException(nameof(propertyFilter)); - } - - return ModelBindingHelper.TryUpdateModelAsync( - model, - prefix, - PageContext, - MetadataProvider, - ModelBinderFactory, - valueProvider, - ObjectValidator, - propertyFilter); - } - - /// - /// Updates the specified instance using values from the 's current - /// and a . - /// - /// The model instance to update. - /// The type of model instance to update. - /// The prefix to use when looking up values in the current . - /// - /// A that on completion returns true if the update is successful. - public virtual async Task TryUpdateModelAsync( - object model, - Type modelType, - string prefix) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - if (modelType == null) - { - throw new ArgumentNullException(nameof(modelType)); - } - - var valueProvider = await CompositeValueProvider.CreateAsync(PageContext, PageContext.ValueProviderFactories); - return await ModelBindingHelper.TryUpdateModelAsync( - model, - modelType, - prefix, - PageContext, - MetadataProvider, - ModelBinderFactory, - valueProvider, - ObjectValidator); - } - - /// - /// Updates the specified instance using the and a - /// . - /// - /// The model instance to update. - /// The type of model instance to update. - /// The prefix to use when looking up values in the . - /// - /// The used for looking up values. - /// A predicate which can be used to filter properties at runtime. - /// A that on completion returns true if the update is successful. - public Task TryUpdateModelAsync( - object model, - Type modelType, - string prefix, - IValueProvider valueProvider, - Func propertyFilter) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - if (modelType == null) - { - throw new ArgumentNullException(nameof(modelType)); - } - - if (valueProvider == null) - { - throw new ArgumentNullException(nameof(valueProvider)); - } - - if (propertyFilter == null) - { - throw new ArgumentNullException(nameof(propertyFilter)); - } - - return ModelBindingHelper.TryUpdateModelAsync( - model, - modelType, - prefix, - PageContext, - MetadataProvider, - ModelBinderFactory, - valueProvider, - ObjectValidator, - propertyFilter); - } - - /// - /// Validates the specified instance. - /// - /// The model to validate. - /// true if the is valid; false otherwise. - public virtual bool TryValidateModel( - object model) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - return TryValidateModel(model, prefix: null); - } - - /// - /// Validates the specified instance. - /// - /// The model to validate. - /// The key to use when looking up information in . - /// - /// true if the is valid;false otherwise. - public virtual bool TryValidateModel( - object model, - string prefix) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - ObjectValidator.Validate( - PageContext, - validationState: null, - prefix: prefix ?? string.Empty, - model: model); - return ModelState.IsValid; - } } } diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageBase.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageBase.cs new file mode 100644 index 0000000000..d1616ee965 --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageBase.cs @@ -0,0 +1,1580 @@ +// 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 System; +using System.IO; +using System.Linq.Expressions; +using System.Security.Claims; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.ModelBinding.Internal; +using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; +using Microsoft.AspNetCore.Mvc.Razor; +using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Net.Http.Headers; + +namespace Microsoft.AspNetCore.Mvc.RazorPages +{ + /// + /// A base class for a Razor page. + /// + [PagesBaseClass] + public abstract class PageBase : RazorPageBase, IRazorPage + { + private IObjectModelValidator _objectValidator; + private IModelMetadataProvider _metadataProvider; + private IModelBinderFactory _modelBinderFactory; + + /// + /// The . + /// + public PageContext PageContext { get; set; } + + /// + public override ViewContext ViewContext + { + get => PageContext; + set + { + PageContext = (PageContext)value; + } + } + + /// + /// Gets the . + /// + public HttpContext HttpContext => PageContext?.HttpContext; + + /// + /// Gets the . + /// + public HttpRequest Request => HttpContext?.Request; + + /// + /// Gets the . + /// + public HttpResponse Response => HttpContext?.Response; + + /// + /// Gets the for the executing action. + /// + public RouteData RouteData + { + get + { + return PageContext.RouteData; + } + } + + /// + /// Gets the . + /// + public ModelStateDictionary ModelState => PageContext?.ModelState; + + private IObjectModelValidator ObjectValidator + { + get + { + if (_objectValidator == null) + { + _objectValidator = HttpContext?.RequestServices?.GetRequiredService(); + } + + return _objectValidator; + } + } + + private IModelMetadataProvider MetadataProvider + { + get + { + if (_metadataProvider == null) + { + _metadataProvider = HttpContext?.RequestServices?.GetRequiredService(); + } + + return _metadataProvider; + } + } + + private IModelBinderFactory ModelBinderFactory + { + get + { + if (_modelBinderFactory == null) + { + _modelBinderFactory = HttpContext?.RequestServices?.GetRequiredService(); + } + + return _modelBinderFactory; + } + } + + /// + public override void EnsureRenderedBodyOrSections() + { + throw new NotSupportedException(); + } + + /// + public override void BeginContext(int position, int length, bool isLiteral) + { + const string BeginContextEvent = "Microsoft.AspNetCore.Mvc.Razor.BeginInstrumentationContext"; + + if (DiagnosticSource?.IsEnabled(BeginContextEvent) == true) + { + DiagnosticSource.Write( + BeginContextEvent, + new + { + httpContext = ViewContext, + path = Path, + position = position, + length = length, + isLiteral = isLiteral, + }); + } + } + + /// + public override void EndContext() + { + const string EndContextEvent = "Microsoft.AspNetCore.Mvc.Razor.EndInstrumentationContext"; + + if (DiagnosticSource?.IsEnabled(EndContextEvent) == true) + { + DiagnosticSource.Write( + EndContextEvent, + new + { + httpContext = ViewContext, + path = Path, + }); + } + } + + /// + /// Creates a . + /// + /// The created for the response. + /// + /// The behavior of this method depends on the in use. + /// and + /// are among likely status results. + /// + public virtual ChallengeResult Challenge() + => new ChallengeResult(); + + /// + /// Creates a with the specified authentication schemes. + /// + /// The authentication schemes to challenge. + /// The created for the response. + /// + /// The behavior of this method depends on the in use. + /// and + /// are among likely status results. + /// + public virtual ChallengeResult Challenge(params string[] authenticationSchemes) + => new ChallengeResult(authenticationSchemes); + + /// + /// Creates a with the specified . + /// + /// used to perform the authentication + /// challenge. + /// The created for the response. + /// + /// The behavior of this method depends on the in use. + /// and + /// are among likely status results. + /// + public virtual ChallengeResult Challenge(AuthenticationProperties properties) + => new ChallengeResult(properties); + + /// + /// Creates a with the specified specified authentication schemes and + /// . + /// + /// used to perform the authentication + /// challenge. + /// The authentication schemes to challenge. + /// The created for the response. + /// + /// The behavior of this method depends on the in use. + /// and + /// are among likely status results. + /// + public virtual ChallengeResult Challenge( + AuthenticationProperties properties, + params string[] authenticationSchemes) + => new ChallengeResult(authenticationSchemes, properties); + + /// + /// Creates a object with by specifying a + /// string. + /// + /// The content to write to the response. + /// The created object for the response. + public virtual ContentResult Content(string content) + => Content(content, (MediaTypeHeaderValue)null); + + /// + /// Creates a object with 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. + public virtual ContentResult Content(string content, string contentType) + => Content(content, MediaTypeHeaderValue.Parse(contentType)); + + /// + /// Creates a object with 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. + /// + /// If encoding is provided by both the 'charset' and the parameters, then + /// the parameter is chosen as the final encoding. + /// + public virtual ContentResult Content(string content, string contentType, Encoding contentEncoding) + { + var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(contentType); + mediaTypeHeaderValue.Encoding = contentEncoding ?? mediaTypeHeaderValue.Encoding; + return Content(content, mediaTypeHeaderValue); + } + + /// + /// Creates a object with by specifying a + /// string and a . + /// + /// The content to write to the response. + /// The content type (MIME type). + /// The created object for the response. + public virtual ContentResult Content(string content, MediaTypeHeaderValue contentType) + { + return new ContentResult + { + Content = content, + ContentType = contentType?.ToString() + }; + } + + /// + /// Creates a ( by default). + /// + /// The created for the response. + /// + /// Some authentication schemes, such as cookies, will convert to + /// a redirect to show a login page. + /// + public virtual ForbidResult Forbid() + => new ForbidResult(); + + /// + /// Creates a ( by default) with the + /// specified authentication schemes. + /// + /// The authentication schemes to challenge. + /// The created for the response. + /// + /// Some authentication schemes, such as cookies, will convert to + /// a redirect to show a login page. + /// + public virtual ForbidResult Forbid(params string[] authenticationSchemes) + => new ForbidResult(authenticationSchemes); + + /// + /// Creates a ( by default) with the + /// specified . + /// + /// used to perform the authentication + /// challenge. + /// The created for the response. + /// + /// Some authentication schemes, such as cookies, will convert to + /// a redirect to show a login page. + /// + public virtual ForbidResult Forbid(AuthenticationProperties properties) + => new ForbidResult(properties); + + /// + /// Creates a ( by default) with the + /// specified specified authentication schemes and . + /// + /// used to perform the authentication + /// challenge. + /// The authentication schemes to challenge. + /// The created for the response. + /// + /// Some authentication schemes, such as cookies, will convert to + /// a redirect to show a login page. + /// + public virtual ForbidResult Forbid(AuthenticationProperties properties, params string[] authenticationSchemes) + => new ForbidResult(authenticationSchemes, properties); + + /// + /// Returns a file with the specified as content + /// () and the specified as the Content-Type. + /// + /// The file contents. + /// The Content-Type of the file. + /// The created for the response. + public virtual FileContentResult File(byte[] fileContents, string contentType) + => File(fileContents, contentType, fileDownloadName: null); + + /// + /// Returns a file with the specified as content (), the + /// specified as the Content-Type and the + /// specified as the suggested file name. + /// + /// The file contents. + /// The Content-Type of the file. + /// The suggested file name. + /// The created for the response. + public virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName) + => new FileContentResult(fileContents, contentType) { FileDownloadName = fileDownloadName }; + + /// + /// Returns a file in the specified () + /// with the specified as the Content-Type. + /// + /// The with the contents of the file. + /// The Content-Type of the file. + /// The created for the response. + public virtual FileStreamResult File(Stream fileStream, string contentType) + => File(fileStream, contentType, fileDownloadName: null); + + /// + /// Returns a file in the specified () with the + /// specified as the Content-Type and the + /// specified as the suggested file name. + /// + /// The with the contents of the file. + /// The Content-Type of the file. + /// The suggested file name. + /// The created for the response. + public virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName) + => new FileStreamResult(fileStream, contentType) { FileDownloadName = fileDownloadName }; + + /// + /// Returns the file specified by () with the + /// specified as the Content-Type. + /// + /// The virtual path of the file to be returned. + /// The Content-Type of the file. + /// The created for the response. + public virtual VirtualFileResult File(string virtualPath, string contentType) + => File(virtualPath, contentType, fileDownloadName: null); + + /// + /// Returns the file specified by () with the + /// specified as the Content-Type and the + /// specified as the suggested file name. + /// + /// The virtual path of the file to be returned. + /// The Content-Type of the file. + /// The suggested file name. + /// The created for the response. + public virtual VirtualFileResult File(string virtualPath, string contentType, string fileDownloadName) + => new VirtualFileResult(virtualPath, contentType) { FileDownloadName = fileDownloadName }; + + /// + /// Returns the file specified by () with the + /// specified as the Content-Type. + /// + /// The physical path of the file to be returned. + /// The Content-Type of the file. + /// The created for the response. + public virtual PhysicalFileResult PhysicalFile(string physicalPath, string contentType) + => PhysicalFile(physicalPath, contentType, fileDownloadName: null); + + /// + /// Returns the file specified by () with the + /// specified as the Content-Type and the + /// specified as the suggested file name. + /// + /// The physical path of the file to be returned. + /// The Content-Type of the file. + /// The suggested file name. + /// The created for the response. + public virtual PhysicalFileResult PhysicalFile( + string physicalPath, + string contentType, + string fileDownloadName) + => new PhysicalFileResult(physicalPath, contentType) { FileDownloadName = fileDownloadName }; + + /// + /// Creates a object that redirects + /// () to the specified local . + /// + /// The local URL to redirect to. + /// The created for the response. + public virtual LocalRedirectResult LocalRedirect(string localUrl) + { + if (string.IsNullOrEmpty(localUrl)) + { + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(localUrl)); + } + + return new LocalRedirectResult(localUrl); + } + + /// + /// Creates a object with set to + /// true () using the specified . + /// + /// The local URL to redirect to. + /// The created for the response. + public virtual LocalRedirectResult LocalRedirectPermanent(string localUrl) + { + if (string.IsNullOrEmpty(localUrl)) + { + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(localUrl)); + } + + return new LocalRedirectResult(localUrl, permanent: true); + } + + /// + /// Creates a object with set to + /// false and set to true + /// () using the specified . + /// + /// The local URL to redirect to. + /// The created for the response. + public virtual LocalRedirectResult LocalRedirectPreserveMethod(string localUrl) + { + if (string.IsNullOrEmpty(localUrl)) + { + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(localUrl)); + } + + return new LocalRedirectResult(localUrl: localUrl, permanent: false, preserveMethod: true); + } + + /// + /// Creates a object with set to + /// true and set to true + /// () using the specified . + /// + /// The local URL to redirect to. + /// The created for the response. + public virtual LocalRedirectResult LocalRedirectPermanentPreserveMethod(string localUrl) + { + if (string.IsNullOrEmpty(localUrl)) + { + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(localUrl)); + } + + return new LocalRedirectResult(localUrl: localUrl, permanent: true, preserveMethod: true); + } + + /// + /// Creates an that produces a response. + /// + /// The created for the response. + public virtual NotFoundResult NotFound() + => new NotFoundResult(); + + /// + /// Creates an that produces a response. + /// + /// The created for the response. + public virtual NotFoundObjectResult NotFound(object value) + => new NotFoundObjectResult(value); + + /// + /// Creates a object that renders this page as a view to the response. + /// + /// The created object for the response. + /// + /// Returning a from a page handler method is equivalent to returning void. + /// The view associated with the page will be executed. + /// + public virtual PageResult Page() => new PageResult(this); + + /// + /// Creates a object that redirects to the specified . + /// + /// The URL to redirect to. + /// The created for the response. + public virtual RedirectResult Redirect(string url) + { + if (string.IsNullOrEmpty(url)) + { + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url)); + } + + return new RedirectResult(url); + } + + /// + /// Creates a object with set to true + /// () using the specified . + /// + /// The URL to redirect to. + /// The created for the response. + public virtual RedirectResult RedirectPermanent(string url) + { + if (string.IsNullOrEmpty(url)) + { + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url)); + } + + return new RedirectResult(url, permanent: true); + } + + /// + /// Creates a object with set to false + /// and set to true () + /// using the specified . + /// + /// The URL to redirect to. + /// The created for the response. + public virtual RedirectResult RedirectPreserveMethod(string url) + { + if (string.IsNullOrEmpty(url)) + { + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url)); + } + + return new RedirectResult(url: url, permanent: false, preserveMethod: true); + } + + /// + /// Creates a object with set to true + /// and set to true () + /// using the specified . + /// + /// The URL to redirect to. + /// The created for the response. + public virtual RedirectResult RedirectPermanentPreserveMethod(string url) + { + if (string.IsNullOrEmpty(url)) + { + throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url)); + } + + return new RedirectResult(url: url, permanent: true, preserveMethod: true); + } + + /// + /// Redirects () to the specified action using the . + /// + /// The name of the action. + /// The created for the response. + public virtual RedirectToActionResult RedirectToAction(string actionName) + => 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. + public virtual RedirectToActionResult RedirectToAction(string actionName, object routeValues) + => 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. + public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName) + => 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. + public virtual RedirectToActionResult RedirectToAction( + string actionName, + string controllerName, + object routeValues) + => RedirectToAction(actionName, controllerName, routeValues, fragment: null); + + /// + /// Redirects () to the specified action using the specified + /// , , and . + /// + /// The name of the action. + /// The name of the controller. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToActionResult RedirectToAction( + string actionName, + string controllerName, + string fragment) + => RedirectToAction(actionName, controllerName, routeValues: null, fragment: fragment); + + /// + /// 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 fragment to add to the URL. + /// The created for the response. + public virtual RedirectToActionResult RedirectToAction( + string actionName, + string controllerName, + object routeValues, + string fragment) + => new RedirectToActionResult(actionName, controllerName, routeValues, fragment); + + /// + /// Redirects () to the specified action with + /// set to false and + /// set to true, using the specified , , + /// , and . + /// + /// The name of the action. + /// The name of the controller. + /// The route data to use for generating the URL. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToActionResult RedirectToActionPreserveMethod( + string actionName = null, + string controllerName = null, + object routeValues = null, + string fragment = null) + { + return new RedirectToActionResult( + actionName: actionName, + controllerName: controllerName, + routeValues: routeValues, + permanent: false, + preserveMethod: true, + fragment: fragment); + } + + /// + /// Redirects () to the specified action with + /// set to true using the specified . + /// + /// The name of the action. + /// The created for the response. + 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. + 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. + 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 fragment to add to the URL. + /// The created for the response. + public virtual RedirectToActionResult RedirectToActionPermanent( + string actionName, + string controllerName, + string fragment) + { + return RedirectToActionPermanent(actionName, controllerName, routeValues: null, fragment: fragment); + } + + /// + /// 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. + public virtual RedirectToActionResult RedirectToActionPermanent( + string actionName, + string controllerName, + object routeValues) + { + return RedirectToActionPermanent(actionName, controllerName, routeValues, fragment: 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 fragment to add to the URL. + /// The created for the response. + public virtual RedirectToActionResult RedirectToActionPermanent( + string actionName, + string controllerName, + object routeValues, + string fragment) + { + return new RedirectToActionResult( + actionName, + controllerName, + routeValues, + permanent: true, + fragment: fragment); + } + + /// + /// Redirects () to the specified action with + /// set to true and + /// set to true, using the specified , , + /// , and . + /// + /// The name of the action. + /// The name of the controller. + /// The route data to use for generating the URL. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToActionResult RedirectToActionPermanentPreserveMethod( + string actionName = null, + string controllerName = null, + object routeValues = null, + string fragment = null) + { + return new RedirectToActionResult( + actionName: actionName, + controllerName: controllerName, + routeValues: routeValues, + permanent: true, + preserveMethod: true, + fragment: fragment); + } + + /// + /// Redirects () to the specified route using the specified . + /// + /// The name of the route. + /// The created for the response. + 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. + 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. + public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues) + { + return RedirectToRoute(routeName, routeValues, fragment: null); + } + + /// + /// Redirects () to the specified route using the specified + /// and . + /// + /// The name of the route. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToRouteResult RedirectToRoute(string routeName, string fragment) + { + return RedirectToRoute(routeName, routeValues: null, fragment: fragment); + } + + /// + /// Redirects () to the specified route using the specified + /// , , and . + /// + /// The name of the route. + /// The parameters for a route. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToRouteResult RedirectToRoute( + string routeName, + object routeValues, + string fragment) + { + return new RedirectToRouteResult(routeName, routeValues, fragment); + } + + /// + /// Redirects () to the specified route with + /// set to false and + /// set to true, using the specified , , and . + /// + /// The name of the route. + /// The route data to use for generating the URL. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToRouteResult RedirectToRoutePreserveMethod( + string routeName = null, + object routeValues = null, + string fragment = null) + { + return new RedirectToRouteResult( + routeName: routeName, + routeValues: routeValues, + permanent: false, + preserveMethod: true, + fragment: fragment); + } + + /// + /// Redirects () to the specified route with + /// set to true using the specified . + /// + /// The name of the route. + /// The created for the response. + 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. + 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. + public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, object routeValues) + { + return RedirectToRoutePermanent(routeName, routeValues, fragment: null); + } + + /// + /// Redirects () to the specified route with + /// set to true using the specified + /// and . + /// + /// The name of the route. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, string fragment) + { + return RedirectToRoutePermanent(routeName, routeValues: null, fragment: fragment); + } + + /// + /// Redirects () to the specified route with + /// set to true using the specified , + /// , and . + /// + /// The name of the route. + /// The parameters for a route. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToRouteResult RedirectToRoutePermanent( + string routeName, + object routeValues, + string fragment) + => new RedirectToRouteResult(routeName, routeValues, permanent: true, fragment: fragment); + + /// + /// Redirects () to the specified route with + /// set to true and + /// set to true, using the specified , , and . + /// + /// The name of the route. + /// The route data to use for generating the URL. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToRouteResult RedirectToRoutePermanentPreserveMethod( + string routeName = null, + object routeValues = null, + string fragment = null) + { + return new RedirectToRouteResult( + routeName: routeName, + routeValues: routeValues, + permanent: true, + preserveMethod: true, + fragment: fragment); + } + + /// + /// Redirects () to the current page. + /// + /// The . + public virtual RedirectToPageResult RedirectToPage() + => RedirectToPage(pageName: null); + + /// + /// Redirects () to the current page with the specified . + /// + /// The parameters for a route. + /// The . + public virtual RedirectToPageResult RedirectToPage(object routeValues) + => RedirectToPage(pageName: null, routeValues: routeValues); + + /// + /// Redirects () to the specified . + /// + /// The name of the page. + /// The . + public virtual RedirectToPageResult RedirectToPage(string pageName) + => RedirectToPage(pageName, routeValues: null); + + /// + /// Redirects () to the specified + /// using the specified . + /// + /// The name of the page. + /// The page handler to redirect to. + /// The . + public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler) + => RedirectToPage(pageName, routeValues: null); + + /// + /// Redirects () to the specified + /// using the specified . + /// + /// The name of the page. + /// The parameters for a route. + /// The . + public virtual RedirectToPageResult RedirectToPage(string pageName, object routeValues) + => RedirectToPage(pageName, pageHandler: null, routeValues: routeValues, fragment: null); + + /// + /// Redirects () to the specified + /// using the specified . + /// + /// The name of the page. + /// The page handler to redirect to. + /// The fragment to add to the URL. + /// The . + public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler, string fragment) + => RedirectToPage(pageName, pageHandler, routeValues: null, fragment: fragment); + + /// + /// Redirects () to the specified + /// using the specified and . + /// + /// The name of the page. + /// The page handler to redirect to. + /// The parameters for a route. + /// The fragment to add to the URL. + /// The . + public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler, object routeValues, string fragment) + => new RedirectToPageResult(pageName, pageHandler, routeValues, fragment); + + /// + /// Redirects () to the specified . + /// + /// The name of the page. + /// The with set. + public virtual RedirectToPageResult RedirectToPagePermanent(string pageName) + => RedirectToPagePermanent(pageName, pageHandler: null); + + /// + /// Redirects () to the specified + /// using the specified . + /// + /// The name of the page. + /// The parameters for a route. + /// The with set. + public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, object routeValues) + => RedirectToPagePermanent(pageName, pageHandler: null, routeValues: routeValues, fragment: null); + + /// + /// Redirects () to the specified + /// using the specified . + /// + /// The name of the page. + /// The page handler to redirect to. + /// The with set. + public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler) + => RedirectToPagePermanent(pageName, pageHandler, routeValues: null); + + /// + /// Redirects () to the specified + /// using the specified . + /// + /// The name of the page. + /// The page handler to redirect to. + /// The parameters for a route. + /// The with set. + public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler, object routeValues) + => RedirectToPagePermanent(pageName, pageHandler, routeValues, fragment: null); + + /// + /// Redirects () to the specified + /// using the specified . + /// + /// The name of the page. + /// The page handler to redirect to. + /// The fragment to add to the URL. + /// The with set. + public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler, string fragment) + => RedirectToPagePermanent(pageName, pageHandler, routeValues: null, fragment: fragment); + + /// + /// Redirects () to the specified + /// using the specified and . + /// + /// The name of the page. + /// The page handler to redirect to. + /// The parameters for a route. + /// The fragment to add to the URL. + /// The with set. + protected RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler, object routeValues, string fragment) + => new RedirectToPageResult(pageName, pageHandler, routeValues, permanent: true, fragment: fragment); + + /// + /// Redirects () to the specified page with + /// set to false and + /// set to true, using the specified , , and . + /// + /// The name of the page. + /// The page handler to redirect to. + /// The route data to use for generating the URL. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToPageResult RedirectToPagePreserveMethod( + string pageName = null, + string pageHandler = null, + object routeValues = null, + string fragment = null) + { + return new RedirectToPageResult( + pageName: pageName, + pageHandler: pageHandler, + routeValues: routeValues, + permanent: false, + preserveMethod: true, + fragment: fragment); + } + + /// + /// Redirects () to the specified route with + /// set to true and + /// set to true, using the specified , , and . + /// + /// The name of the page. + /// The page handler to redirect to. + /// The route data to use for generating the URL. + /// The fragment to add to the URL. + /// The created for the response. + public virtual RedirectToPageResult RedirectToPagePermanentPreserveMethod( + string pageName = null, + string pageHandler = null, + object routeValues = null, + string fragment = null) + { + return new RedirectToPageResult( + pageName: pageName, + pageHandler: pageHandler, + routeValues: routeValues, + permanent: true, + preserveMethod: true, + fragment: fragment); + } + + /// + /// Creates a with the specified authentication scheme. + /// + /// The containing the user claims. + /// The authentication scheme to use for the sign-in operation. + /// The created for the response. + public virtual SignInResult SignIn(ClaimsPrincipal principal, string authenticationScheme) + => new SignInResult(authenticationScheme, principal); + + /// + /// Creates a with the specified specified authentication scheme and + /// . + /// + /// The containing the user claims. + /// used to perform the sign-in operation. + /// The authentication scheme to use for the sign-in operation. + /// The created for the response. + public virtual SignInResult SignIn( + ClaimsPrincipal principal, + AuthenticationProperties properties, + string authenticationScheme) + => new SignInResult(authenticationScheme, principal, properties); + + /// + /// Creates a with the specified authentication schemes. + /// + /// The authentication schemes to use for the sign-out operation. + /// The created for the response. + public virtual SignOutResult SignOut(params string[] authenticationSchemes) + => new SignOutResult(authenticationSchemes); + + /// + /// Creates a with the specified specified authentication schemes and + /// . + /// + /// used to perform the sign-out operation. + /// The authentication scheme to use for the sign-out operation. + /// The created for the response. + public virtual SignOutResult SignOut(AuthenticationProperties properties, params string[] authenticationSchemes) + => new SignOutResult(authenticationSchemes, properties); + + /// + /// Creates a object by specifying a . + /// + /// The status code to set on the response. + /// The created object for the response. + public virtual StatusCodeResult StatusCode(int statusCode) + => new StatusCodeResult(statusCode); + + /// + /// Creates a object by specifying a and + /// + /// The status code to set on the response. + /// The value to set on the . + /// The created object for the response. + public virtual ObjectResult StatusCode(int statusCode, object value) + => new ObjectResult(value) { StatusCode = statusCode }; + + /// + /// Creates an that produces an response. + /// + /// The created for the response. + public virtual UnauthorizedResult Unauthorized() + => new UnauthorizedResult(); + + /// + /// Updates the specified instance using values from the 's current + /// . + /// + /// The type of the model object. + /// The model instance to update. + /// A that on completion returns true if the update is successful. + public virtual Task TryUpdateModelAsync( + TModel model) + where TModel : class + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + return TryUpdateModelAsync(model, prefix: string.Empty); + } + + /// + /// Updates the specified instance using values from the 's current + /// and a . + /// + /// The type of the model object. + /// The model instance to update. + /// The prefix to use when looking up values in the current . + /// + /// A that on completion returns true if the update is successful. + public virtual async Task TryUpdateModelAsync( + TModel model, + string prefix) + where TModel : class + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (prefix == null) + { + throw new ArgumentNullException(nameof(prefix)); + } + var valueProvider = await CompositeValueProvider.CreateAsync(PageContext, PageContext.ValueProviderFactories); + return await TryUpdateModelAsync(model, prefix, valueProvider); + } + + /// + /// Updates the specified instance using the and a + /// . + /// + /// The type of the model object. + /// The model instance to update. + /// The prefix to use when looking up values in the . + /// + /// The used for looking up values. + /// A that on completion returns true if the update is successful. + public virtual Task TryUpdateModelAsync( + TModel model, + string prefix, + IValueProvider valueProvider) + where TModel : class + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (prefix == null) + { + throw new ArgumentNullException(nameof(prefix)); + } + + if (valueProvider == null) + { + throw new ArgumentNullException(nameof(valueProvider)); + } + + return ModelBindingHelper.TryUpdateModelAsync( + model, + prefix, + PageContext, + MetadataProvider, + ModelBinderFactory, + valueProvider, + ObjectValidator); + } + + /// + /// Updates the specified instance using values from the 's current + /// and a . + /// + /// The type of the model object. + /// The model instance to update. + /// The prefix to use when looking up values in the current . + /// + /// (s) which represent top-level properties + /// which need to be included for the current model. + /// A that on completion returns true if the update is successful. + public async Task TryUpdateModelAsync( + TModel model, + string prefix, + params Expression>[] includeExpressions) + where TModel : class + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (includeExpressions == null) + { + throw new ArgumentNullException(nameof(includeExpressions)); + } + + var valueProvider = await CompositeValueProvider.CreateAsync(PageContext, PageContext.ValueProviderFactories); + return await ModelBindingHelper.TryUpdateModelAsync( + model, + prefix, + PageContext, + MetadataProvider, + ModelBinderFactory, + valueProvider, + ObjectValidator, + includeExpressions); + } + + /// + /// Updates the specified instance using values from the 's current + /// and a . + /// + /// The type of the model object. + /// The model instance to update. + /// The prefix to use when looking up values in the current . + /// + /// A predicate which can be used to filter properties at runtime. + /// A that on completion returns true if the update is successful. + public async Task TryUpdateModelAsync( + TModel model, + string prefix, + Func propertyFilter) + where TModel : class + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (propertyFilter == null) + { + throw new ArgumentNullException(nameof(propertyFilter)); + } + + var valueProvider = await CompositeValueProvider.CreateAsync(PageContext, PageContext.ValueProviderFactories); + return await ModelBindingHelper.TryUpdateModelAsync( + model, + prefix, + PageContext, + MetadataProvider, + ModelBinderFactory, + valueProvider, + ObjectValidator, + propertyFilter); + } + + /// + /// Updates the specified instance using the and a + /// . + /// + /// The type of the model object. + /// The model instance to update. + /// The prefix to use when looking up values in the . + /// + /// The used for looking up values. + /// (s) which represent top-level properties + /// which need to be included for the current model. + /// A that on completion returns true if the update is successful. + public Task TryUpdateModelAsync( + TModel model, + string prefix, + IValueProvider valueProvider, + params Expression>[] includeExpressions) + where TModel : class + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (valueProvider == null) + { + throw new ArgumentNullException(nameof(valueProvider)); + } + + if (includeExpressions == null) + { + throw new ArgumentNullException(nameof(includeExpressions)); + } + + return ModelBindingHelper.TryUpdateModelAsync( + model, + prefix, + PageContext, + MetadataProvider, + ModelBinderFactory, + valueProvider, + ObjectValidator, + includeExpressions); + } + + /// + /// Updates the specified instance using the and a + /// . + /// + /// The type of the model object. + /// The model instance to update. + /// The prefix to use when looking up values in the . + /// + /// The used for looking up values. + /// A predicate which can be used to filter properties at runtime. + /// A that on completion returns true if the update is successful. + public Task TryUpdateModelAsync( + TModel model, + string prefix, + IValueProvider valueProvider, + Func propertyFilter) + where TModel : class + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (valueProvider == null) + { + throw new ArgumentNullException(nameof(valueProvider)); + } + + if (propertyFilter == null) + { + throw new ArgumentNullException(nameof(propertyFilter)); + } + + return ModelBindingHelper.TryUpdateModelAsync( + model, + prefix, + PageContext, + MetadataProvider, + ModelBinderFactory, + valueProvider, + ObjectValidator, + propertyFilter); + } + + /// + /// Updates the specified instance using values from the 's current + /// and a . + /// + /// The model instance to update. + /// The type of model instance to update. + /// The prefix to use when looking up values in the current . + /// + /// A that on completion returns true if the update is successful. + public virtual async Task TryUpdateModelAsync( + object model, + Type modelType, + string prefix) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (modelType == null) + { + throw new ArgumentNullException(nameof(modelType)); + } + + var valueProvider = await CompositeValueProvider.CreateAsync(PageContext, PageContext.ValueProviderFactories); + return await ModelBindingHelper.TryUpdateModelAsync( + model, + modelType, + prefix, + PageContext, + MetadataProvider, + ModelBinderFactory, + valueProvider, + ObjectValidator); + } + + /// + /// Updates the specified instance using the and a + /// . + /// + /// The model instance to update. + /// The type of model instance to update. + /// The prefix to use when looking up values in the . + /// + /// The used for looking up values. + /// A predicate which can be used to filter properties at runtime. + /// A that on completion returns true if the update is successful. + public Task TryUpdateModelAsync( + object model, + Type modelType, + string prefix, + IValueProvider valueProvider, + Func propertyFilter) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (modelType == null) + { + throw new ArgumentNullException(nameof(modelType)); + } + + if (valueProvider == null) + { + throw new ArgumentNullException(nameof(valueProvider)); + } + + if (propertyFilter == null) + { + throw new ArgumentNullException(nameof(propertyFilter)); + } + + return ModelBindingHelper.TryUpdateModelAsync( + model, + modelType, + prefix, + PageContext, + MetadataProvider, + ModelBinderFactory, + valueProvider, + ObjectValidator, + propertyFilter); + } + + /// + /// Validates the specified instance. + /// + /// The model to validate. + /// true if the is valid; false otherwise. + public virtual bool TryValidateModel( + object model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + return TryValidateModel(model, prefix: null); + } + + /// + /// Validates the specified instance. + /// + /// The model to validate. + /// The key to use when looking up information in . + /// + /// true if the is valid;false otherwise. + public virtual bool TryValidateModel( + object model, + string prefix) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + ObjectValidator.Validate( + PageContext, + validationState: null, + prefix: prefix ?? string.Empty, + model: model); + return ModelState.IsValid; + } + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs index 187c97fce6..bbe0df2d4e 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs @@ -56,11 +56,6 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages } } - /// - /// Gets the instance this model belongs to. - /// - public Page Page => PageContext?.Page; - /// /// Gets the . /// @@ -798,6 +793,12 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages public virtual NotFoundObjectResult NotFound(object value) => new NotFoundObjectResult(value); + /// + /// Creates a object that renders the page. + /// + /// The . + public virtual PageResult Page() => new PageResult(PageContext.Page, this); + /// /// Returns the file specified by () with the /// specified as the Content-Type. @@ -1546,15 +1547,6 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages public virtual UnauthorizedResult Unauthorized() => new UnauthorizedResult(); - /// - /// Creates a object that renders the page. - /// - /// The . - protected internal PageViewResult View() - { - return new PageViewResult(Page); - } - /// /// Validates the specified instance. /// diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageViewResult.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageResult.cs similarity index 77% rename from src/Microsoft.AspNetCore.Mvc.RazorPages/PageViewResult.cs rename to src/Microsoft.AspNetCore.Mvc.RazorPages/PageResult.cs index 20595e54ab..0529cf1be4 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageViewResult.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageResult.cs @@ -11,23 +11,23 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages /// /// An that renders a Razor Page. /// - public class PageViewResult : ActionResult + public class PageResult : ActionResult { /// - /// Initializes a new instance of . + /// Initializes a new instance of . /// - /// The to render. - public PageViewResult(Page page) + /// The to render. + public PageResult(PageBase page) { Page = page; } /// - /// Initializes a new instance of with the specified . + /// Initializes a new instance of with the specified . /// - /// The to render. + /// The to render. /// The page model. - public PageViewResult(Page page, object model) + public PageResult(PageBase page, object model) { Page = page; Model = model; @@ -46,7 +46,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages /// /// Gets the to execute. /// - public Page Page { get; } + public PageBase Page { get; } /// /// Gets or sets the HTTP status code. diff --git a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/PageActionInvokerTest.cs b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/PageActionInvokerTest.cs index 8ea8d6a64b..045d99901d 100644 --- a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/PageActionInvokerTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/PageActionInvokerTest.cs @@ -674,7 +674,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal _executeAction = executeAction; } - public override Task ExecuteAsync(PageContext pageContext, PageViewResult result) + public override Task ExecuteAsync(PageContext pageContext, PageResult result) => _executeAction(pageContext); } diff --git a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageModelTest.cs b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageModelTest.cs index 0a7c09f8d1..db34bff3b6 100644 --- a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageModelTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/PageModelTest.cs @@ -1422,7 +1422,6 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages }; // Act & Assert - Assert.Same(page, pageModel.Page); Assert.Same(pageContext, pageModel.PageContext); Assert.Same(pageContext, pageModel.ViewContext); Assert.Same(httpContext, pageModel.HttpContext); @@ -1491,10 +1490,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages }; // Act - var result = pageModel.View(); + var result = pageModel.Page(); // Assert - var pageResult = Assert.IsType(result); + var pageResult = Assert.IsType(result); Assert.Same(page, pageResult.Page); } diff --git a/test/WebSites/RazorPagesWebSite/HandlerTestPage.cshtml b/test/WebSites/RazorPagesWebSite/HandlerTestPage.cshtml index 2c74b182e3..e5ceaf4fe7 100644 --- a/test/WebSites/RazorPagesWebSite/HandlerTestPage.cshtml +++ b/test/WebSites/RazorPagesWebSite/HandlerTestPage.cshtml @@ -8,14 +8,14 @@ public IActionResult OnGet() { - return View(); + return Page(); } public async Task OnPostAsync() { await TaskCache.CompletedTask; MethodName = nameof(OnPostAsync); - return View(); + return Page(); } public async Task OnGetCustomer() diff --git a/test/WebSites/RazorPagesWebSite/ModelHandlerTestModel.cs b/test/WebSites/RazorPagesWebSite/ModelHandlerTestModel.cs index 62ff94c90c..7d9e57388d 100644 --- a/test/WebSites/RazorPagesWebSite/ModelHandlerTestModel.cs +++ b/test/WebSites/RazorPagesWebSite/ModelHandlerTestModel.cs @@ -14,14 +14,14 @@ namespace RazorPagesWebSite public IActionResult OnGet() { - return View(); + return Page(); } public async Task OnPostAsync() { await TaskCache.CompletedTask; MethodName = nameof(OnPostAsync); - return View(); + return Page(); } public async Task OnGetCustomer() diff --git a/test/WebSites/RazorPagesWebSite/OnGetView.cshtml b/test/WebSites/RazorPagesWebSite/OnGetView.cshtml index 14b2bb2ca4..7ffc9efe71 100644 --- a/test/WebSites/RazorPagesWebSite/OnGetView.cshtml +++ b/test/WebSites/RazorPagesWebSite/OnGetView.cshtml @@ -5,7 +5,7 @@ { Message = "From OnGet"; - return View(); + return Page(); } public string Message { get; set; } = "Default"; } diff --git a/test/WebSites/RazorPagesWebSite/Pages/TagHelper/PostWithHandler.cshtml b/test/WebSites/RazorPagesWebSite/Pages/TagHelper/PostWithHandler.cshtml index 039373f9a1..2b5067e71a 100644 --- a/test/WebSites/RazorPagesWebSite/Pages/TagHelper/PostWithHandler.cshtml +++ b/test/WebSites/RazorPagesWebSite/Pages/TagHelper/PostWithHandler.cshtml @@ -4,12 +4,12 @@ { public IActionResult OnPostEdit(int id) { - return View(); + return Page(); } public IActionResult OnPostDelete(int id) { - return View(); + return Page(); } } diff --git a/test/WebSites/RazorPagesWebSite/TempData/TempDataPageModel.cs b/test/WebSites/RazorPagesWebSite/TempData/TempDataPageModel.cs index 805286e3e9..e2a3ebe605 100644 --- a/test/WebSites/RazorPagesWebSite/TempData/TempDataPageModel.cs +++ b/test/WebSites/RazorPagesWebSite/TempData/TempDataPageModel.cs @@ -14,13 +14,13 @@ namespace RazorPagesWebSite.TempData public IActionResult OnGet() { - return View(); + return Page(); } public IActionResult OnPost() { Message = "Secret post"; - return View(); + return Page(); } } }