Linked Questions
41 questions linked to/from ASP.NET Core Web API exception handling
91 votes
7 answers
59k views
ASP.NET Core equivalent of ASP.NET MVC 5's HttpException
In ASP.NET MVC 5 you could throw a HttpException with a HTTP code and this would set the response like so: throw new HttpException((int)HttpStatusCode.BadRequest, "Bad Request."); HttpException does ...
47 votes
7 answers
76k views
Error handling (Sending ex.Message to the client)
I have an ASP.NET Core 1.0 Web API application and trying to figure out how to pass the exception message to the client if a function that my controller is calling errors out. I have tried so many ...
25 votes
1 answer
20k views
How do I customize ASP.Net Core model binding errors?
I would like to return only standardized error responses from my Web API (Asp.net Core 2.1), but I can't seem to figure out how to handle model binding errors. The project is just created from the "...
32 votes
2 answers
51k views
Handling exception in asp.net core?
I have asp.net core application. The implementation of configure method redirects the user to "Error" page when there is an exception ( in non Development environment) However it only works if the ...
21 votes
1 answer
10k views
Inspect DefaultHttpContext body in unit test situation
I'm trying to use the DefaultHttpContext object to unit test my exception handling middleware. My test method looks like this: [Fact] public async Task ...
17 votes
2 answers
38k views
How can I throw an exception in an ASP.NET Core WebAPI controller that returns an object?
In Framework WebAPI 2, I have a controller that looks like this: [Route("create-license/{licenseKey}")] public async Task<LicenseDetails> CreateLicenseAsync(string licenseKey, ...
15 votes
4 answers
15k views
Why is ASP.NET Core's app.UseExceptionHandler() not working?
I have in my startup.cs file: if (env.IsDevelopment()) { app.UseExceptionHandler("/api/error/error-local-development"); } else { app....
9 votes
3 answers
21k views
ASP.Net Core exception handling middleware
I am trying to use middleware for exception handling in my ASP.Net Core 3.0 Web API project: public class ErrorHandlingMiddleware { private readonly RequestDelegate next; public ...
2 votes
2 answers
5k views
ASP.Net Core Error JSON
I'm playing a bit with ASP.NET Core. I'm creating a basic webapi. I'd like to show a JSON error when there is a problem. The printscreen shows want I want on my screen. The only problem is that it's ...
3 votes
3 answers
6k views
How to throwing 404 on bad .NET Core API route?
I have a .NET Core web app which has an API. I've defined an Middleware class based on this answer like so: public class ErrorHandlingMiddleware { private readonly RequestDelegate next; ...
3 votes
3 answers
4k views
Custom Selective 404 Page in MVC6
I am looking for help we can have a custom Error404 page in ASP.NET 5 MVC6. The closest I have come is to use app.UseStatusCodePagesWithReExecute("/Error/{0}"); in the Startup.cs Configure method. ...
3 votes
3 answers
3k views
Exception not going to ExceptionFilter after referencing Standard lib
I have a traditional WebApi project on .NET 4.6.1 with a global ExceptionFilter which handles known exceptions to return a friendly message with a corresponding status code. WebApiConfig.cs public ...
1 vote
1 answer
4k views
Redirect outside of the Controllers context in ASP.NET Core
I do not know if this is actually possible, but I think it' s worth a try to find out. There are maybe other and better patterns (if you know one let me know, I will look them up) to do this, but I'm ...
6 votes
1 answer
3k views
Web API Core 3.1 ExceptionHandler middleware throws 'cannot write to closed stream' while writing to HttpContext.Response
I have a simple Web API Core v3.1 where I am trying to handle Exceptions Globally. After following this answer https://stackoverflow.com/a/55166404/1508398, here is my code for doing that. app....
3 votes
1 answer
5k views
ASP.NET Core startup exceptions handling
When the code in the startup class throws, ASP.NET Core catches the exception instead of letting it fall through behind the IWebHost.Build call. The app consequently does start but only serves an ...