Home Computer Hardware How to use the ProblemDetails middleware in ASP.NET Core

How to use the ProblemDetails middleware in ASP.NET Core

by admin
How to use the ProblemDetails middleware in ASP.NET Core


For an API to be maintainable and usable there should be consistency in the way responses are sent to the clients. This article talks about ProblemDetails, open source ASP.NET Core middleware from Kristian Hellang that can be used to generate detailed results for the exceptions that occur in your application.

To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here

Also on InfoWorld: Rapid UI development with Flutter for Windows ]

Create an ASP.NET Core MVC project in Visual Studio 2019

First off, let’s create an ASP.NET Core project in Visual Studio 2019. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new ASP.NET Core project in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web Application” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window, specify the name and location for the new project.
  6. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences.
  7. Click Create.
  8. In the “Create a New ASP.NET Core Web Application” window shown next, select .NET Core as the runtime and ASP.NET Core 3.1 (or later) from the drop-down list at the top.
  9. Select “Web Application (Model-View-Controller)” as the project template to create a new ASP.NET Core MVC application. 
  10. Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be using those features here.
  11. Ensure that Authentication is set to “No Authentication” as we won’t be using authentication either.
  12. Click Create.

This will create a new ASP.NET Core MVC project in Visual Studio 2019. Next, create a new ApiController class named ValuesController. We’ll use this class and the project in the sections that follow.

What is ProblemDetails? Why do we need it?

One of the goals of REST APIs is consistency, i.e., being able to emit responses in a consistent manner. When working with these APIs you might often need to define response formats for the errors that occur in your application. While you could use HTTP Status Codes for this purpose, you will often want to communicate more detailed information to the clients than they provide.

For example, consider the failure of a payment in a shopping cart application. The payment might have failed for any number of reasons, such as insufficient bank funds, incorrect credit card information, wrong one-time password code, or a failure in the transaction processing system. Hence, it is imperative that there is a standard way in which these error messages can be sent to the client.

Copyright © 2020 IDG Communications, Inc.



Source link

You may also like

Leave a Comment