diff --git a/modules/docs/app/Volo.DocsTestApp/DocsTestAppModule.cs b/modules/docs/app/Volo.DocsTestApp/DocsTestAppModule.cs index 275eb0f713..40a6e86b81 100644 --- a/modules/docs/app/Volo.DocsTestApp/DocsTestAppModule.cs +++ b/modules/docs/app/Volo.DocsTestApp/DocsTestAppModule.cs @@ -3,8 +3,11 @@ using System.Diagnostics; using System.Globalization; using System.IO; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -100,13 +103,17 @@ namespace Volo.DocsTestApp { options.DefaultThemeName = BasicTheme.Name; }); + + Configure(options => + { + options.Conventions.AddPageRoute("/Error", "error/{statusCode}"); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); - - app.UseDeveloperExceptionPage(); + var env = context.GetEnvironment(); app.UseVirtualFiles(); @@ -119,8 +126,15 @@ namespace Volo.DocsTestApp app.UseAuthentication(); app.UseRequestLocalization(app.ApplicationServices.GetRequiredService>().Value); - - app.UseStatusCodePagesWithReExecute("/error/{0}"); + + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseStatusCodePagesWithReExecute("/error/{0}"); + } app.UseMvc(routes => { diff --git a/modules/docs/app/Volo.DocsTestApp/Pages/Error.cshtml b/modules/docs/app/Volo.DocsTestApp/Pages/Error.cshtml new file mode 100644 index 0000000000..6166957b3a --- /dev/null +++ b/modules/docs/app/Volo.DocsTestApp/Pages/Error.cshtml @@ -0,0 +1,11 @@ +@page +@model Volo.DocsTestApp.Pages.ErrorModel +@{ +
+
+            @Model.ErrorMessage
+        
+ + Home Page +
+} diff --git a/modules/docs/app/Volo.DocsTestApp/Controllers/ErrorController.cs b/modules/docs/app/Volo.DocsTestApp/Pages/Error.cshtml.cs similarity index 65% rename from modules/docs/app/Volo.DocsTestApp/Controllers/ErrorController.cs rename to modules/docs/app/Volo.DocsTestApp/Pages/Error.cshtml.cs index 943a3a85f2..bbdea77925 100644 --- a/modules/docs/app/Volo.DocsTestApp/Controllers/ErrorController.cs +++ b/modules/docs/app/Volo.DocsTestApp/Pages/Error.cshtml.cs @@ -1,43 +1,59 @@ -using System; +using System; using System.Collections.Generic; using System.Net; +using System.Threading.Tasks; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Mvc; using Serilog; -using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; +using Volo.Abp.Domain.Entities; -namespace Volo.DocsTestApp.Controllers +namespace Volo.DocsTestApp.Pages { - public class ErrorController : AbpController + public class ErrorModel : AbpPageModel { - [Route("error/{statusCode}")] - [HttpGet] - public IActionResult Index(int statusCode = 0) + public string ErrorMessage { get; set; } + + public async Task OnGetAsync(string statusCode) { + if (!int.TryParse(statusCode, out var errorStatusCode)) + { + errorStatusCode = (int)HttpStatusCode.BadRequest; + } + var statusFeature = HttpContext.Features.Get(); if (statusFeature != null) { Log.Warning("Handled {0} error for URL: {1}", statusCode, statusFeature.OriginalPath); } - var isValidStatusCode = Enum.IsDefined(typeof(HttpStatusCode), statusCode); - if (!isValidStatusCode) + var exceptionHandlerPathFeature = HttpContext.Features.Get(); + if (exceptionHandlerPathFeature != null) { - statusCode = (int)HttpStatusCode.BadRequest; + var exception = exceptionHandlerPathFeature.Error; + var path = exceptionHandlerPathFeature.Path; + + if (exception is EntityNotFoundException) + { + ErrorMessage = exception.Message; + return Page(); + } } - return new ContentResult + var isValidStatusCode = Enum.IsDefined(typeof(HttpStatusCode), errorStatusCode); + if (!isValidStatusCode) { - ContentType = System.Net.Mime.MediaTypeNames.Text.Html, - StatusCode = statusCode, - Content = string.Format(HtmlBody, _errorMessages.ContainsKey(statusCode) - ? _errorMessages[statusCode] - : "Looks like something went wrong!") - }; - } + errorStatusCode = (int)HttpStatusCode.BadRequest; + } - private const string HtmlBody = ""; + ErrorMessage = _errorMessages.ContainsKey(errorStatusCode) + ? _errorMessages[errorStatusCode] + : "Looks like something went wrong!"; + + return Page(); + } + #region Error Messages /*For more ASCII arts http://patorjk.com/software/taag/#p=display&h=0&f=Big&t=400*/ private readonly Dictionary _errorMessages = new Dictionary { @@ -84,7 +100,7 @@ This is a forbidden area!" | | | |_| | | | |_| \___/ |_| -We can't find the page you're looking for..." +Hmm, we couldn't find the page you're looking for..." }, { 500, @@ -123,5 +139,6 @@ Ooops! Our server is experiencing a mild case of the hiccups." Looks like we're having some server issues." } }; + #endregion } -} +} \ No newline at end of file