<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Akshita</title>
    <description>The latest articles on DEV Community by Akshita (@akshita_sharma).</description>
    <link>https://dev.to/akshita_sharma</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2620614%2Fdbeb00f0-8ee3-4d01-9280-aef47b3f9f5e.png</url>
      <title>DEV Community: Akshita</title>
      <link>https://dev.to/akshita_sharma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akshita_sharma"/>
    <language>en</language>
    <item>
      <title>How to Use Problem Details in ASP.NET Core APIs</title>
      <dc:creator>Akshita</dc:creator>
      <pubDate>Tue, 31 Dec 2024 08:17:28 +0000</pubDate>
      <link>https://dev.to/akshita_sharma/how-to-use-problem-details-in-aspnet-core-apis-23jk</link>
      <guid>https://dev.to/akshita_sharma/how-to-use-problem-details-in-aspnet-core-apis-23jk</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Problem Details&lt;/strong&gt;&lt;br&gt;
Problem details is a format that is used for specifying errors in HTTP APIs. This format is a machine readable format that a developer can use in API responses. It can ease development and for end user, it is easier to understand the specific error. For instance, when a user tries to save any form and there is some error in between, in such case Problem Details can be helpful to throw the right error instead of telling them a 404 or 500 internal server error.&lt;/p&gt;

&lt;p&gt;For a developer When developing HTTP APIs, it is crucial to provide consistent and informative error responses for smooth developer experience. ASP.NET Core's Problem Details is a perfect standardized solution to this challenge by ensuring your APIs communicate errors effectively and uniformly. &lt;/p&gt;

&lt;p&gt;The objective of this standard is to make error response more informative and actionable, not just for human developers, but for the systems that consume APIs at runtime.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Details Format&lt;/strong&gt;&lt;br&gt;
Problem Details contains the following.&lt;br&gt;
&lt;strong&gt;type&lt;/strong&gt;: A URI reference to identify the problem type. It provides human operators a place to find more information about the error. &lt;br&gt;
&lt;strong&gt;title&lt;/strong&gt;: A human readable summary of the problem type.&lt;br&gt;
&lt;strong&gt;status&lt;/strong&gt;: The HTTP status code generated on the origin of problem.&lt;br&gt;
&lt;strong&gt;detail&lt;/strong&gt;: A human-readable explanation specific to this occurrence of the problem. &lt;br&gt;
&lt;strong&gt;instance&lt;/strong&gt;: A URI reference to identifies the problem occurrence.&lt;/p&gt;

&lt;p&gt;To get some more understanding of Problem Details let's create a sample project with Product Controller and an action method which throws an unhandled exception.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehmc3clvutezofngyl66.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehmc3clvutezofngyl66.png" alt="Image description" width="637" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now run your project and check the exception which will look like as follows&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmv40acmspdj0yqu6dlmr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmv40acmspdj0yqu6dlmr.png" alt="Image description" width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ASP.NET Core has built-in support for Problem Details, can be enabled by calling the AddProblemDetails method in Program.cs file. This method registers the problem details middleware to handle exceptions and return a problem details response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu4r6vktczpmt760tyytl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu4r6vktczpmt760tyytl.png" alt="Image description" width="337" height="41"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We need to enable one more middleware using the UseExceptionHandler method that converts unhandled exceptions into Problem Details responses.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvp190nclf3z1f426rsh0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvp190nclf3z1f426rsh0.png" alt="Image description" width="244" height="36"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's run the API now to see the difference.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1f14o2qw1bgxwdoxwbid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1f14o2qw1bgxwdoxwbid.png" alt="Image description" width="800" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally call the UseStatusCodePages method to add a middleware to return a problem details response for common HTTP status codes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfrlr8l34znm08e0gynh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfrlr8l34znm08e0gynh.png" alt="Image description" width="251" height="37"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now calling any random API that does not exists we will get this response&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepkyyi8ea97yro0rlvyh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepkyyi8ea97yro0rlvyh.png" alt="Image description" width="688" height="110"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting custom response using Probelm Details in ASP.Net Core&lt;/strong&gt;&lt;br&gt;
If you want to get some other information in the problem details response and want to make it to use globally, you can use ProblemDetails object like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febeo9lv9p17fw1x9m641.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febeo9lv9p17fw1x9m641.png" alt="Image description" width="800" height="511"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here TryHandleAsync method that will be invoked by the problem details middleware when any exception is thrown.&lt;/p&gt;

&lt;p&gt;We have map some other exceptions such as BadHttpRequestException and UnauthorizedAccessException with appropriate HTTP status codes. We can map other specific exceptions in the same way as per our requirement. &lt;br&gt;
We defined ProblemDetails object with title, status code and other details and returned in HTTP response. We have added additional information like traceId and requestId to be more informative.&lt;/p&gt;

&lt;p&gt;Now register this custom exception handler in Program.cs file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fijamwuzkc5spk8ewod5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fijamwuzkc5spk8ewod5w.png" alt="Image description" width="558" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, run the API and see the results&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faohngtki5apyobfdyiah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faohngtki5apyobfdyiah.png" alt="Image description" width="800" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting Problem Details in Web API Response&lt;/strong&gt;&lt;br&gt;
Here is the expample how you can use Problem Details in response of any Rest API&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqd6czmgvo8c83boq1efw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqd6czmgvo8c83boq1efw.png" alt="Image description" width="789" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here Problem() is a built-in method that will ingest information into ProblemDetails object and return it in response. Here is the result after running the API&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmghrhlpge8qcmg7wnn9o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmghrhlpge8qcmg7wnn9o.png" alt="Image description" width="800" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Problem Details is a great way to define API exception or errors in such a way that a developer and a machine both can easily understand. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
