/// <summary> ///Maps data to an empty view model for a 404 page. /// </summary> /// <param name="viewModel">The view model to map data to.</param> /// <param name="mappingParameters">The data passed through to map to the view model.</param> public virtual void MapNotFoundPageViewModel( INotFoundPageViewModel viewModel, NotFoundPageViewModelBuilderParameters mappingParameters ) { viewModel.PageTitle = "Page not found"; viewModel.MetaDescription = "Sorry, that page could not be found"; }
/// <summary> /// Creates and maps data to a view model for a 404 page. /// </summary> /// <param name="mappingParameters">The data passed through to map to the view model.</param> public virtual Task <INotFoundPageViewModel> BuildNotFoundPageViewModelAsync( NotFoundPageViewModelBuilderParameters mappingParameters ) { var viewModel = _pageViewModelFactory.CreateNotFoundPageViewModel(); _pageViewModelMapper.MapNotFoundPageViewModel(viewModel, mappingParameters); return(Task.FromResult(viewModel)); }
/// <summary> /// Creates and maps data to a view model for a 404 page. /// </summary> /// <param name="mappingParameters">The data passed through to map to the view model.</param> public virtual async Task <INotFoundPageViewModel> BuildNotFoundPageViewModelAsync( NotFoundPageViewModelBuilderParameters mappingParameters ) { var viewModel = _pageViewModelFactory.CreateNotFoundPageViewModel(); await _pageViewModelMapper.MapNotFoundPageViewModelAsync(viewModel, mappingParameters); return(viewModel); }
private static NotFoundPageViewModelBuilderParameters GetViewModelBuilerParameters(Controller controller) { var request = controller.Request; var feature = controller.HttpContext.Features.Get <IStatusCodeReExecuteFeature>(); var vmParams = new NotFoundPageViewModelBuilderParameters() { Path = feature?.OriginalPath ?? request.Path, PathBase = feature?.OriginalPathBase ?? request.PathBase, QueryString = feature?.OriginalQueryString ?? request.QueryString.Value }; return(vmParams); }
/// <summary> /// If a page isnt found, check to see if we have a redirection rule /// in place for the url. /// </summary> private async Task <ActionResult> GetRewriteResultAsync(NotFoundPageViewModelBuilderParameters vmParameters) { var query = new GetRewriteRuleSummaryByPathQuery() { Path = vmParameters.Path }; var rewriteRule = await _queryExecutor.ExecuteAsync(query); if (rewriteRule != null) { return(new RedirectResult(rewriteRule.WriteTo, true)); } return(null); }
/// <summary> /// Use this in your controllers to return a 404 result using the Cofoundry custom 404 page system. This /// has the added benefit of checking for Rewrite Rules and automatically redirecting. /// </summary> public async Task <ActionResult> GetViewAsync() { var path = HttpContext.Current.Request.Path; var result = await GetRewriteResultAsync(path); if (result != null) { return(result); } var vmParams = new NotFoundPageViewModelBuilderParameters(path); var vm = await _pageViewModelBuilder.BuildNotFoundPageViewModelAsync(vmParams); HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotFound; return(new ViewResult() { ViewName = "NotFound", ViewData = new ViewDataDictionary(vm) }); }
/// <summary> /// Maps data to an empty view model for a 404 page. /// </summary> /// <param name="viewModel">The view model to map data to.</param> /// <param name="mappingParameters">The data passed through to map to the view model.</param> public virtual Task MapNotFoundPageViewModelAsync( INotFoundPageViewModel viewModel, NotFoundPageViewModelBuilderParameters mappingParameters ) { if (viewModel == null) { throw new ArgumentNullException(nameof(viewModel)); } if (mappingParameters == null) { throw new ArgumentNullException(nameof(mappingParameters)); } viewModel.PageTitle = "Not found"; viewModel.MetaDescription = "Sorry, that page could not be found"; viewModel.Path = mappingParameters.Path; viewModel.PathBase = mappingParameters.PathBase; viewModel.QueryString = mappingParameters.QueryString; viewModel.StatusCode = 404; viewModel.StatusCodeDescription = "Not Found"; return(Task.CompletedTask); }