// 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.RazorPages.Infrastructure; using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNetCore.Mvc.RazorPages { [PageModelAttribute] public abstract class PageModel { private IModelMetadataProvider _metadataProvider; private IModelBinderFactory _modelBinderFactory; private IObjectModelValidator _objectValidator; private ITempDataDictionary _tempData; private IUrlHelper _urlHelper; /// /// Gets the . /// [PageContext] public PageContext PageContext { get; set; } /// /// 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 => PageContext.RouteData; /// /// Gets the . /// public ModelStateDictionary ModelState => PageContext.ModelState; /// /// Gets the for user associated with the executing action. /// public ClaimsPrincipal User => HttpContext?.User; /// /// Gets or sets used by . /// public ITempDataDictionary TempData { get { if (_tempData == null) { var factory = HttpContext?.RequestServices?.GetRequiredService(); _tempData = factory?.GetTempData(HttpContext); } return _tempData; } set { if (value == null) { throw new ArgumentNullException(nameof(value)); } _tempData = value; } } /// /// Gets or sets the . /// public IUrlHelper Url { get { if (_urlHelper == null) { var factory = HttpContext?.RequestServices?.GetRequiredService(); _urlHelper = factory?.GetUrlHelper(PageContext); } return _urlHelper; } set { if (value == null) { throw new ArgumentNullException(nameof(value)); } _urlHelper = value; } } /// /// Gets or sets used by . /// public ViewDataDictionary ViewData => PageContext?.ViewData; 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; } } /// /// 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. protected internal Task TryUpdateModelAsync(TModel model) where TModel : class { if (model == null) { throw new ArgumentNullException(nameof(model)); } return TryUpdateModelAsync(model, name: string.Empty); } /// /// Updates the specified instance using values from the 's current /// . /// /// The type of the model object. /// The model instance to update. /// The model name. /// A that on completion returns true if the update is successful. protected internal async Task TryUpdateModelAsync(TModel model, string name) where TModel : class { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } var valueProvider = await CompositeValueProvider.CreateAsync(PageContext, PageContext.ValueProviderFactories); return await TryUpdateModelAsync(model, name, valueProvider); } /// /// Updates the specified instance using the and a /// . /// /// The type of the model object. /// The model instance to update. /// The name 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. protected internal Task TryUpdateModelAsync( TModel model, string name, IValueProvider valueProvider) where TModel : class { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } if (valueProvider == null) { throw new ArgumentNullException(nameof(valueProvider)); } return ModelBindingHelper.TryUpdateModelAsync( model, name, 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 name 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. protected internal async Task TryUpdateModelAsync( TModel model, string name, 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, name, 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 name 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. protected internal async Task TryUpdateModelAsync( TModel model, string name, 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, name, 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 name 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. protected internal Task TryUpdateModelAsync( TModel model, string name, 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, name, 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 name 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. protected internal Task TryUpdateModelAsync( TModel model, string name, 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, name, 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 name to use when looking up values in the current . /// /// A that on completion returns true if the update is successful. protected internal async Task TryUpdateModelAsync( object model, Type modelType, string name) { 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, name, 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 name 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. protected internal Task TryUpdateModelAsync( object model, Type modelType, string name, 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, name, PageContext, MetadataProvider, ModelBinderFactory, valueProvider, ObjectValidator, propertyFilter); } /// /// 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 }; /// /// 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 the page. /// /// The . public virtual PageResult Page() => new PageResult(); /// /// 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 . /// /// The URL to redirect to. /// The created for the response. protected internal 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 pageModel. /// 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 pageModel. /// 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 pageModel. /// 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 pageModel. /// 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) { return new RedirectToActionResult(actionName, controllerName, routeValues, fragment) { UrlHelper = Url, }; } /// /// 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 pageModel. /// 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) { UrlHelper = Url, }; } /// /// 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) => 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) => 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 pageModel. /// The created for the response. public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName) => 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 pageModel. /// The fragment to add to the URL. /// The created for the response. public virtual RedirectToActionResult RedirectToActionPermanent( string actionName, string controllerName, string fragment) => 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 pageModel. /// The parameters for a route. /// The created for the response. public virtual RedirectToActionResult RedirectToActionPermanent( string actionName, string controllerName, object routeValues) => 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 pageModel. /// 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) { UrlHelper = Url, }; } /// /// 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 pageModel. /// 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) { UrlHelper = Url, }; } /// /// Redirects () to the specified route using the specified . /// /// The name of the route. /// The created for the response. public virtual RedirectToRouteResult RedirectToRoute(string routeName) => 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) => 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) => 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) => 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) { UrlHelper = Url, }; } /// /// 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) { UrlHelper = Url, }; } /// /// 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) => 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) => 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) => 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) => 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) { return new RedirectToRouteResult(routeName, routeValues, permanent: true, fragment: fragment) { UrlHelper = Url, }; } /// /// 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) { UrlHelper = Url, }; } /// /// 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, pageHandler, routeValues: null); /// /// 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 . public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler, object routeValues) => RedirectToPage(pageName, pageHandler, routeValues, fragment: 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 . /// /// 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 . /// /// The name of the page. /// The parameters for a route. /// The fragment to add to the URL. /// The with set. public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, object routeValues, string fragment) => RedirectToPagePermanent(pageName, pageHandler: null, routeValues: routeValues, 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. public virtual 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) { return new ObjectResult(value) { StatusCode = statusCode }; } /// /// Creates an that produces an response. /// /// The created for the response. public virtual UnauthorizedResult Unauthorized() => new UnauthorizedResult(); /// /// 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, name: 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 name) { if (model == null) { throw new ArgumentNullException(nameof(model)); } ObjectValidator.Validate( PageContext, validationState: null, prefix: name ?? string.Empty, model: model); return ModelState.IsValid; } } }