Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Exception Handling Asp.Net Core

Date- Aug 05,2020

20683

Exception Handling

Error Handling In Asp.Net Core :-

Error Handling in Asp.Net Core has changed a bit from what we used to use in Asp.Net Mvc. Before .Net Core We used to use OnException Methods to handle Exceptions while executing actions But it doesn't see working in .Net Core as many features are changed.So In this Article we will see how to make exception handling work  in Asp.Net Core.

Setting Up Error Logs:-  

Unlike Asp.Net Mvc in Asp.Net Core we have to do most of the work with our startup.cs file.Here in Configure Method We can use Exception Handler like we have showed in code snippet.This will write exception in a separated Log File.


Open your startup.cs and use exception handling code block like this.


public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
  if (env.IsDevelopment()) {
    app.UseExceptionHandler(a =>a.Run(async context =>{
      var exceptionHandlerPathFeature = context.Features.Get < IExceptionHandlerPathFeature > ();
      var exception = exceptionHandlerPathFeature.Error;
      if (!Directory.Exists(env.ContentRootPath + "\\App_Data\\log\\")) {
        Directory.CreateDirectory(env.ContentRootPath + "\\App_Data\\log\\");
      }
      var filename = env.ContentRootPath + "\\App_Data\\" + "log\\" + "Logerror.txt";
      var sw = new System.IO.StreamWriter(filename, true);
      sw.WriteLine(DateTime.Now.ToString() + " " + exception.Message + " " + exception.InnerException + " " + exception.StackTrace);
      sw.Close();
      var result = JsonConvert.SerializeObject(new {
        error = exception.Message
      });
      context.Response.ContentType = "application/json";
      await context.Response.WriteAsync(result);
    }));
  } else {
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
  }
  app.UseHttpsRedirection();
  app.UseStaticFiles();
  app.UseCookiePolicy();
  app.UseMvc(routes =>{
    routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
  });
}

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Mastering Exception Handling in Java: A Comprehensive Guide
Mar 16, 2026
Mastering Exception Handling in C#: A Comprehensive Guide
Mar 16, 2026

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page