Skip to content

The example shows two approaches on how to handle errors in the ASP.NET Core Dashboard application

License

Notifications You must be signed in to change notification settings

DevExpress-Examples/asp-net-core-dashboard-handle-errors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Files to look at:

ASP.NET Core Dashboard - How to specify custom exception text

The dashboard in this project contains invalid data connection. This example shows how to override the default text in the exception that occurs when a controller tries to load data.

Implement the IExceptionFilter interface to create a custom exception filter and specify a custom exception message. The displayed text depends on whether the application is in development mode:

public class CustomExceptionFilter : IExceptionFilter { internal bool isDevelopmentMode = false; public CustomExceptionFilter(IWebHostEnvironment hostingEnvironment) { this.isDevelopmentMode = hostingEnvironment.IsDevelopment(); } string GetJson(string message) { return $"{{ \"Message\":\"{message}\" }}"; } public virtual void OnException(ExceptionContext context) { if(context.ExceptionHandled || context.Exception == null) { return; } context.Result = new ContentResult { Content = GetJson(!isDevelopmentMode ? "Custom exception text for end users" : "Custom exception text for developers"), ContentType = "application/json", StatusCode = (int)HttpStatusCode.BadRequest }; context.ExceptionHandled = true; } }

Create a custom controller that uses the custom exception filter:

namespace AspNetCoreDashboard.ExceptionOnDataLoading.Controllers { [TypeFilter(typeof(CustomExceptionFilter))] public class CustomDashboardController : DashboardController { public CustomDashboardController(DashboardConfigurator configurator) : base(configurator) { } } }

Specify the CustomDashboard controller when you configure endpoints:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // ... app.UseEndpoints(endpoints => { EndpointRouteBuilderExtension.MapDashboardRoute(endpoints, "api/dashboards", "CustomDashboard"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}" ); }); app.UseStaticFiles(); app.UseDevExpressControls(); }

About

The example shows two approaches on how to handle errors in the ASP.NET Core Dashboard application

Topics

Resources

License

Stars

Watchers

Forks

Contributors 7