HTTP API Problem Details in ASP.NET Core

Problem Details in ASP.NET CoreIf you’ve been writing HTTP API’s, you likely have needed to return exceptions and errors back to the consuming clients.  Before you go and grow your own solution to this common problem, there is RFC 7807 Problem Details for HTTP APIs which set out solve this problem.

“problem detail” as a way to carry machine-readable details of errors in a HTTP response to avoid the need to define new error response formats for HTTP APIs.

Problem Details in ASP.NET Core

Now that you’re on board with Problem Details, how do we use or implement this in ASP.NET Core.  Thankfully, Kristian Hellang has done all the heavy lifting and released Hellang.Middleware.ProblemDetails on NuGet.

Hellang.Middleware.ProblemDetails

Once you get the NuGet package, it’s simply a matter of adding it to your Startup’s Configure(IApplicationBuilder app):

By default with no configuration, this will return a Problem Details body and application/problem+json content type for status codes returned from MVC. This means 404’s as well as status codes you return from your controller actions explicitly.

Here’s an example of the response for a 404 when a route is not found.

For making explicit returns such as UnauthorizedResult:

This returns, as you might expect:

Bad Request

Most often you might be return a BadRequestObjectResult when the users sends malformed request.  In this case the solution is pretty sweet.  Simply provide BadRequest() with an object that is or derives from Microsoft.AspNetCore.Mvc.ProblemDetails

ProblemDetailsException

Another option is throw an exception.  Specifically you can throw a ProblemDetailsException that takes a ProblemDetails as a parameter.

Mapping Exceptions to ProblemDetails

Although I’m not in love with throwing a ProblemDetailsException, one of the reasons is you may not be directly inside an MVC Controller action.  If you follow my blog enough, you know I like to decouple using something like the MediatR library.  In this case you likely won’t be throwing a ProblemDetailsException.

But no worries, this situation is covered by being able to map exceptions to ProblemDetailsExceptions in the configuration.

IncludeExceptionDetails

If a 500 occurs due to an unhandled exception, this is covered.  However you likely want to see the exception details when in development.  Again, no problem!

Sample

This post really revolves around the sample on the GitHub repo.  Check it out to try it out yourself.

Are you using another solution for ProblemDetails in your HTTP API’s?  Let me know in the comments or on Twitter.

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

Roundup #7: AspNet Core Nested Applications, Blazor-State, DebugType=Embedded, Controllers as action filters

Here are the things that caught my eye last week.  I’d love to hear what you found most interesting this week.  Let me know in the comments or on Twitter.

AspNet Core Nested Applications

Given any application of a reasonable size, to reason about it and manage complexity one generally applies modular programming along clear and well defined boundaries. Recently I was seeking to do this with AspNet Core where I wanted to compose several independent applications, potentially developed by separate teams, within the one host.

I’ve been wanting to do something similar in AspNetCore so this is right up my alley with great timing.  This is a great method for having independent apps but running under one host.

Link: http://dhickey.ie/2018/06/09/aspnet-core-nested-apps/

 

Blazor-State

If you are familiar with MediatRRedux, or the Command Pattern you will feel right at home. All of the behaviors are written as plug-ins/middle-ware and attached to the MediatR pipeline. You can pick and choose which behaviors you would like to use or even write your own.

Pretty interesting idea and like to see where this goes along with Blazer in the future.

Link: https://timewarpengineering.github.io/blazor-state/

 

<DebugType>embedded</DebugType>

This was really cool and I had no idea about this.  You can also set this from Visual Studio in the Project Properties, Build, Advanced window.

Link: https://twitter.com/KirillOsenkov/status/1007052524946255872

 

Controllers as action filters in ASP.NET Core MVC

It is common to leverage action filters when building MVC applications – this was the case in classic ASP.NET MVC, in ASP.NET Web API and is a still widely used technique (with much richer support!) in ASP.NET Core MVC.

What is not commonly known though, is that it’s possible for controllers to act as their own filters – so let’s have a look at this feature today.

Really love this idea of adding the filters to the controller itself since they are generally coupled anyways.

Link: https://www.strathweb.com/2018/06/controllers-as-action-filters-in-asp-net-core-mvc/

 

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

Roundup #6: Fuget.org, Nat Friedman AMA, Lippert and Nishanov, 10 Things I Regret About Node.js, Avoiding Microservice Megadisasters

Here are the things that caught my eye last week.  I’d love to hear what you found most interesting this week.  Let me know in the comments or on Twitter.

fuget.org

Have you ever wondered what exactly is in a nuget to see if it’s right for you? You read the description, you like the name, but, if you’re like me, you probably ended up in GitHub reading the source code to decide if you want to use the library.

This announced last week but after I posted last week round up.  What a straight awesome idea.  Incredible.

Link: http://praeclarum.org/post/174440517348/introducing-fugetorg

 

I’m Nat Friedman, future CEO of GitHub. AMA.

Hi, I’m Nat Friedman, future CEO of GitHub (when the deal closes at the end of the year). I’m here to answer your questions about the planned acquisition, and Microsoft’s work with developers and open source. Ask me anything

After the big announcement on Monday of Microsoft acquiring GitHub, it’s pretty interesting to read some of the Q&A.

Link: https://www.reddit.com/r/AMA/comments/8pc8mf/im_nat_friedman_future_ceo_of_github_ama/

 

Eric Lippert and Gor Nishanov: Co-Routines, Probabilistic Programming, and More

Eric Lippert (@ericlippert) and Gor Nishanov (@GorNishanov) discuss C++ co-routines, probabilistic programming, undefined behavior, and more. Eric Lippert was the first dev interviewee on @Ch9. In fact, it was his interview that really made us believe that we were on to something with unscripted conversations in video format… Great to see Eric again! He still has the boyish looks and sly grin. Amazing, the things he and team are working on, which should have impact across the industry. Gor loves C++ and he is quite the personality. His co-routine design is on the path to standardization in C++20. We (Service Fabric team) use his current implementation in many places, including code that runs in the Windows kernel. We love co-routines!. We must get him to spend more time on camera and share his passionate insights.

Link: https://www.youtube.com/watch?v=AZYCPoG7rD0

 

10 Things I Regret About Node.js – Ryan Dahl – JSConf EU 2018

Pretty interesting listening to the gripes about Node and interesting to see the prototype project Deno with TypeScript.

Link: https://www.youtube.com/watch?v=M3BM9TB-8yA

 

Avoiding Microservice Megadisasters – Jimmy Bogard

You’ve spent months re-architecting your monolith into the new microservices vision. Everyone gathers around to flip the switch. You navigate to the first page…and nothing happens. Refresh…still nothing. The site is so slow, it won’t respond for minutes. What happened? In this session, I’ll walk through a post-mortem of a real-life microservice disaster. I’ll show the modeling, development and production problems we found, and how we slowly morphed the new distributed monolith into our final picture of sanity. While we can’t prevent project failures, we can at least identify problems early on in our design so that our final product results in a clean, robust distributed system.

Link: https://www.youtube.com/watch?v=gfh-VCTwMw8

 

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.