{"id":1751,"date":"2023-06-14T08:55:05","date_gmt":"2023-06-14T08:55:05","guid":{"rendered":"http:\/\/waqar-arshad.com\/?p=1751"},"modified":"2023-06-14T08:55:05","modified_gmt":"2023-06-14T08:55:05","slug":"asp-net-core-exception-handling","status":"publish","type":"post","link":"http:\/\/waqar-arshad.com\/index.php\/2023\/06\/14\/asp-net-core-exception-handling\/","title":{"rendered":"Asp.Net Core Exception Handling"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1751\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><div class=\"pld-dislike-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-dislike-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1751\" data-trigger-type=\"dislike\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-down\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-dislike-count-wrap pld-count-wrap\"><\/span>\r\n<\/div><\/div>\n<h1 class=\"wp-block-heading\">ASP.NET Core &#8211; Exception Handling<\/h1>\n\n\n\n<p>Exception handling is one of the most important features of any application. Fortunately, ASP.NET Core includes a middleware that makes exception handling easy. In this chapter, we will learn about exception handling in ASP.NET Core application.<\/p>\n\n\n\n<p>By default, ASP.NET Core returns a simple status code for any exception that occurs in an application. Consider the following example of Configure method which throws an error.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>class<code> <\/code>Startup<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>public<code> <\/code>void<code> Configure(<\/code>IApplicationBuilder<code> app, <\/code>IHostingEnvironment<code> env)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app.Run(context =&gt; { <\/code>throw<code> <\/code>new<code> <\/code>Exception<code>(<\/code>\"error\"<code>); });<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>The above code will display the following result.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"358\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture38.png\" alt=\"\" class=\"wp-image-1752\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture38.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture38-300x178.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/unhandled-exception-page.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install Microsoft.AspNetCore.Diagnostics Package<\/h2>\n\n\n\n<p>To handle exceptions and display user friendly messages, we need to install&nbsp;<code>Microsoft.AspNetCore.Diagnostics<\/code>&nbsp;NuGet package and add middleware in the&nbsp;<code>Configure()<\/code>&nbsp;method. If you are using Visual Studio templates to create ASP.NET Core application then this package might be already installed. If not then you can add&nbsp;<code>Microsoft.AspNetCore.Diagnostics<\/code>&nbsp;package via NuGet manager.<\/p>\n\n\n\n<p>The&nbsp;<code>Microsoft.AspNetCore.Diagnostics<\/code>&nbsp;package includes following extension methods to handle exceptions in different scenario:<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\">\n<li>UseDeveloperExceptionPage<\/li>\n\n\n\n<li>UseExceptionHandler<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>UseDeveloperExceptionPage<\/strong><\/h3>\n\n\n\n<p>The&nbsp;<code>UseDeveloperExceptionPage<\/code>&nbsp;extension method adds middleware into the request pipeline which displays developer friendly exception detail page. This helps developers in tracing errors that occur during development phase.<\/p>\n\n\n\n<p>As this middleware displays sensitive information, it is advisable to add it only in development environment.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>class<code> <\/code>Startup<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>public<code> <\/code>void<code> Configure(<\/code>IApplicationBuilder<code> app, <\/code>IHostingEnvironment<code> env)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>if<code> (env.IsDevelopment() || env.IsStaging())<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.UseDeveloperExceptionPage();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.Run(context =&gt; { <\/code>throw<code> <\/code>new<code> <\/code>Exception<code>(<\/code>\"error\"<code>); });<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>The above code will display the following result.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"377\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture39.png\" alt=\"\" class=\"wp-image-1753\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture39.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture39-300x188.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/exception.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>Exception Handling<\/p>\n\n\n\n<p>As you can see above, the developer exception page includes 4 tabs: Stack, Query, Cookies, and Headers. Stack tab displays information of stack trace, which indicates where exactly an error occurred. Query tab displays information about query string. Cookies tab displays information about cookies set by the request and Headers tab displays information about headers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>UseExceptionHandler<\/strong><\/h3>\n\n\n\n<p>In MVC Core application, we might want some other controller to handle all exceptions and display custom user friendly error messages. The&nbsp;<code>UseExceptionHandler<\/code>&nbsp;extension method allows us to configure custom error handling route. This is useful when an application runs under production environment.<\/p>\n\n\n\n<p>Example: Exception Handler in MVC<\/p>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>void<code> Configure(<\/code>IApplicationBuilder<code> app, <\/code>IHostingEnvironment<code> env)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>if<code> (env.IsDevelopment() || env.IsStaging())<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.UseDeveloperExceptionPage();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>else<code> <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.UseExceptionHandler(<\/code>\"\/Home\/Error\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>\/\/code removed for clarity <code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>In the above example, the&nbsp;<code>UseExceptionHandler(\"\/Home\/Error\")<\/code>&nbsp;sets the error handler path. If an error occurred in the MVC application then it will redirect the request to&nbsp;<code>\/home\/error<\/code>, which will execute the Error action method of&nbsp;<code>HomeController<\/code>.<\/p>\n\n\n\n<p>Create a simple Error action method in&nbsp;<code>HomeController<\/code>&nbsp;class as shown below.<\/p>\n\n\n\n<p>HomeController:<\/p>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>class<code> <\/code>HomeController<code> : <\/code>Controller<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>public<code> HomeController()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>public<code> <\/code>IActionResult<code> Error()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>return<code> View();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; } <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>\/\/ other code removed for the clarity<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>The following is the content of Error.cshtml.<\/p>\n\n\n\n<p>Error.cshtml<\/p>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; ViewData&#91;\"Title\"] = \"Error\";<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;h1<code> <\/code>class=\"text-danger\"&gt;<code>Error.<\/code>&lt;\/h1&gt;<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;h2<code> <\/code>class=\"text-danger\"&gt;<code>An error occurred while processing your request.<\/code>&lt;\/h2&gt;<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;h3&gt;<code>Development Mode<\/code>&lt;\/h3&gt;<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;p&gt;<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; Swapping to <\/code>&lt;strong&gt;<code>Development<\/code>&lt;\/strong&gt;<code> environment will display more detailed information about the error that occurred.<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;\/p&gt;<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;p&gt;<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>&lt;strong&gt;<code>Development environment should not be enabled in deployed applications<\/code>&lt;\/strong&gt;<code>, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the <\/code>&lt;strong&gt;<code>ASPNETCORE_ENVIRONMENT<\/code>&lt;\/strong&gt;<code> environment variable to <\/code>&lt;strong&gt;<code>Development<\/code>&lt;\/strong&gt;<code>, and restarting the application.<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;\/p&gt;<code><\/code><\/pre>\n\n\n\n<p>Now, when an error occurs, it displays the page shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"339\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture40.png\" alt=\"\" class=\"wp-image-1754\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture40.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture40-300x169.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/custom-exception-page.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>Exception Handling<\/p>\n\n\n\n<p>Thus, we can configure middleware to handle exceptions in ASP.NET Core application.<\/p>\n\n\n\n<p>&nbsp;Note:<\/p>\n\n\n\n<p>Visual Studio automatically creates Error.cshtml under Home folder when you create ASP.NET Core project with MVC template.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET Core &#8211; Exception Handling Exception handling is one of the most important features of any application. Fortunately, ASP.NET Core includes a middleware that makes exception handling easy. In this chapter, we will learn about exception handling in ASP.NET Core application. By default, ASP.NET Core returns a simple status code for any exception that occurs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1751","post","type-post","status-publish","format-standard","hentry","category-asp-net-core"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"admin","author_link":"http:\/\/waqar-arshad.com\/index.php\/author\/waqar_29_1\/"},"uagb_comment_info":20,"uagb_excerpt":"ASP.NET Core &#8211; Exception Handling Exception handling is one of the most important features of any application. Fortunately, ASP.NET Core includes a middleware that makes exception handling easy. In this chapter, we will learn about exception handling in ASP.NET Core application. By default, ASP.NET Core returns a simple status code for any exception that occurs&hellip;","_links":{"self":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/comments?post=1751"}],"version-history":[{"count":1,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1751\/revisions"}],"predecessor-version":[{"id":1755,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1751\/revisions\/1755"}],"wp:attachment":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/media?parent=1751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/categories?post=1751"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/tags?post=1751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}