All Projects → semack → AspNetCore.FriendlyExceptions

semack / AspNetCore.FriendlyExceptions

Licence: MIT license
ASP.NET Core Filter and Middleware to catch exceptions and translate them into nice HTTP responses

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to AspNetCore.FriendlyExceptions

Demo.AspNetCore.WebApi
Sample Web API powered by ASP.NET Core MVC, Azure Cosmos DB and MediatR
Stars: ✭ 24 (+41.18%)
Mutual labels:  asp-net-core, asp-net-core-mvc, asp-net-core-web-api
Smartbreadcrumbs
A utility library for ASP.NET Core (both MVC and Razor Pages) websites to easily add and customize breadcrumbs.
Stars: ✭ 113 (+564.71%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Mytested.aspnetcore.mvc
Fluent testing library for ASP.NET Core MVC.
Stars: ✭ 1,358 (+7888.24%)
Mutual labels:  asp-net-core, asp-net-core-mvc
XAF Security E4908
This repository contains examples for Role-based Access Control, Permission Management, and OData / Web / REST API Services for Entity Framework and XPO ORM
Stars: ✭ 47 (+176.47%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Znetcs.aspnetcore.authentication.basic
A simple basic authentication middleware.
Stars: ✭ 40 (+135.29%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Banksystem
ASP.NET Core banking system with secure communication capability between instances, cards, secure payments, etc.
Stars: ✭ 70 (+311.76%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Sio.core
✔ [ SIOC ] Swastika I/O Core is an all in one platform (e.g CMS, eCommerce, Forum, Q&A, CRM...) ASP.NET Core / Dotnet Core System based on SIOH Framework.
Stars: ✭ 121 (+611.76%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Equinoxproject
Full ASP.NET Core 5 application with DDD, CQRS and Event Sourcing concepts
Stars: ✭ 5,120 (+30017.65%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Jquerydatatablesserverside
Asp.Net Core Server Side for Jquery DataTables Multiple Column Filtering and Sorting with Pagination and Excel Export
Stars: ✭ 191 (+1023.53%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Coolcat
A sample about how to create a dynamic plugins mechanism with ASP.NET Core Mvc at runtime. This sample is based on .NET Core 3.1 and .NET 5
Stars: ✭ 216 (+1170.59%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Nlog.web
NLog integration for ASP.NET & ASP.NET Core 1-5
Stars: ✭ 252 (+1382.35%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Demo.aspnetcore.mvc.cosmosdb
Sample Web API powered by ASP.NET Core MVC, Azure Cosmos DB and MediatR
Stars: ✭ 19 (+11.76%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Electron.net
Build cross platform desktop apps with ASP.NET Core (Razor Pages, MVC, Blazor).
Stars: ✭ 6,074 (+35629.41%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Piranha.core
Piranha CMS is the friendly editor-focused CMS for .NET Core that can be used both as an integrated CMS or as a headless API.
Stars: ✭ 1,242 (+7205.88%)
Mutual labels:  asp-net-core, asp-net-core-mvc
X.pagedlist
Library for easily paging through any IEnumerable/IQueryable in ASP.NET/ASP.NET Core
Stars: ✭ 625 (+3576.47%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Puck Core
Open source, cross platform .NET Core CMS. Fast, scalable, code-first, unobtrusive and extensible with powerful querying and Lucene integration.
Stars: ✭ 115 (+576.47%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Demo.Ndjson.AsyncStreams
Sample project for demonstrating how to use async streams and NDJSON to improve user experience by streaming JSON objects from server to client and client to server in .NET
Stars: ✭ 30 (+76.47%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Professionalcsharp7
Code samples for the book Professional C# 7 and .NET Core 2.0 (with updates for 2.1), Wrox Press
Stars: ✭ 403 (+2270.59%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Aspnet.mvc.typedrouting
A collection of extension methods providing strongly typed routing and link generation for ASP.NET Core MVC projects.
Stars: ✭ 460 (+2605.88%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Elmahcore
ELMAH for Net.Standard and Net.Core
Stars: ✭ 127 (+647.06%)
Mutual labels:  asp-net-core, asp-net-core-mvc

ASP.NET Core Friendly Exceptions Filter and Middleware Build Status

A filter and middleware that can translate exceptions into nice http resonses. This allows you to throw meaningfull exceptions from your framework, business code or other middlewares and translate the exceptions to nice and friendly http responses.

Authors

This code based on Owin Friendly Exceptions Middleware created by Anders Åberg and has been adapted for ASP.NET Core usage by Andriy S'omak.

Installation

Before using of the library Nuget Package must be installed.

Install-Package AspNetCore.FriendlyExceptions

Examples of usage

There are a two ways of using of the library: using ExceptionFilter or registering Midlleware. You can choose any of them.

Configuration

Add transformation rules to the Startup.cs file.

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            services.AddFriendlyExceptionsTransforms(options =>
            {
                options.Transforms = TransformsCollectionBuilder.Begin()
    
                    .Map<ExampleException>()
                    .To(HttpStatusCode.BadRequest, "This is the reasonphrase",
                        ex => "And this is the response content: " + ex.Message)
    
                    .Map<SomeCustomException>()
                    .To(HttpStatusCode.NoContent, "Bucket is emtpy", ex => string.Format("Inner details: {0}", ex.Message))
    
                    .Map<EntityUnknownException>()
                    .To(HttpStatusCode.NotFound, "Entity does not exist", ex => ex.Message)
    
                    .Map<InvalidAuthenticationException>()
                    .To(HttpStatusCode.Unauthorized, "Invalid authentication", ex => ex.Message)
    
                    .Map<AuthorizationException>()
                    .To(HttpStatusCode.Forbidden, "Forbidden", ex => ex.Message)
                    
                    // Map all other exceptions if needed. 
                    // Also it would be useful if you want to map exception to a known model.
                    .MapAllOthers()
                    .To(HttpStatusCode.InternalServerError, "Internal Server Error", ex => ex.Message)
                    
                    .Done();
            });
            ...
        }

By default, FriendlyExceptions sets the response Content-Type to text/plain. To use a different type:

    .Map<SomeJsonException>()
    .To(HttpStatusCode.BadRequest, "This exception is json",
        ex => JsonConvert.SerializeObject(ex.Message), "application/json")

Using filter

Register ExceptionFilter.

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            services.AddMvc()
                .AddMvcOptions(s => s.Filters.Add(typeof(FriendlyExceptionAttribute)))
            ...
        }                

Add filter attribute to the Controller.

    [Produces("application/json")]
    [FriendlyException]
    public class AccountController : Controller
    {
        ...

Using Middleware

In case you use middleware, the registration method must be at the top of list of all registrations.

        public void Configure(IApplicationBuilder app)
        {
            app.UseFriendlyExceptions();
            ...
        }

Contribute

Contributions are welcome. Just open an Issue or submit a PR.

Contact

You can reach me via my email

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].