our

Latest Blogs

How to use Web.config Custom errors in ASP.NET MVC?

iFour Team -July 05, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  •  
  •  
  •  
How to use Web.config Custom errors in ASP.NET MVC?

What is web.config file in ASP.NET?

The config file is just a system configuration file. You can just think it as of a common file that is by default accessible through the system. Here, you can easily store some important function that can be accessed from anywhere in the project.

Web.config file is an XML-based configuration, this file utilized in an ASP.NET-based application to manage different types of settings that concerned with the configuration of our website. In this way, we will separate our application logic from configuration logic.

What is a custom error in ASP.NET?

Custom error means when error occurs at the same time of error message is displayed by user that is called custom error. If you set custom errors to Off, an in-depth status page is showed all users. If you set custom errors to on, a general page is showed all users.

Asp.net Provides three types of error modes to trace an error. That modes decide whether or not an error message is displayed in custom error remote the only mode is the default mode of displaying error messages.

Off Mode

This mode is use for displaying error messages on a local and remote server in case of an error.

RemoteOnly

This mode is use for displaying error messages on the remote server only in case of an error. By using this mode, we can show our own custom error messages page for specific errors on the remote server only.

RedirectMode

The Redirect mode is an attribute if you want to preserve the original URL without aspxerrorpath query string. By default, that was redirectMode set to ResponseRedirect, and that's why the URL gets changed when an exception occurred.

Set the redirectMode attribute to ResponseRewrite. This will keep the original URL but still display a custom page. Be attentive that ResponseRewrite is only working for .aspx, or .html files, but that was not for action methods. If you want to set action methods for different status codes, keep the ResponseRedirect change value.

The example sets different HTML files for different status codes and redirectMode to ResponseRewrite.

Example

    
    
    

Create 500.html, 404.html, and 400.html files at the root of the application. The above settings will keep the original URL without a query string while showing the inner content of an HTML page with the response status code 200 OK.

Example of the Custom error step by step

1) First go to the file menu -> New Project create new project After that, Click on Ok

Project Select Screen

Fig: Project Select Screen

2) Select the MVC Checkbox then click on the Next button. Select web application and you will get the below display by clicking on ok button

Framework selection Screen

Fig: Framework selection Screen

(3) Create a simple project go to the Web.config File and write a simple below code for understanding the concept of the CustomErrors, change the mode of custom error.




    
        
        
        
        
    
    
        
        
    
    
        
            
                
                
            
            
                
                
            
            
                
                
            
            
                
                
            
            
                
                
            
            
                
                
            
            
                
                
            
        
    
    
        
    
    
        
            
            
        
    


Op zoek naar een vertrouwd ASP.Net-softwareontwikkelingsbedrijf ? Uw zoekopdracht eindigt hier.

(4) Go to the Home controller and write a below code for understanding the concept of the CustomErrors

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BlogCustomError.Controllers
{
    public class HomeController : Controller
    {
        
        public ActionResult Index()
        {
            string msg = null;
            ViewBag.Message = M.Length; // this will throw an exception
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        
    }
}

After that go to the model folder and add new class for Error handling.. Apply the name of class is “FilterConfig” and add below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BlogCustomError.Models
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    }
}

After Change the Mode setting Run the project and you will get the below output.

An error occurred while processing your request this error was written by the user. This functionality has lots of status code for different errors.

Output Screen

Fig: Output Screen

Here you can set your own error message with statusCode. We can set default code for default error like page not found


    
        
        
        
    

This attribute is used to define an https status code which is a numeric value. You can define multiple attributes using multiple error elements.

200 Status code is call when response is ok type of error.

Status Code attribute

301 – This code is use to Moved Permanently

403 – This code is use for Forbidden

404 – This code is use for Not Found

Redirect attribute

This attribute will be used to redirect to a page designed to show messages based on a specific type of error. These specific types can be defined using the status Code.

Conclusion

This blog is used for understanding the concept of custom error. This is used to provide user define error message for specific error on the website. We hope you liked this article on Custom Error, this blog is helpful to you as we have covered about all factors of Custom Error. Main purpose of custom error is to provide the simple understandable error message application.

Work with us

Well do everything we can to make our next best project!

Our Insights

Check out our most recent blogs

An in-depth guide on Angular Dependency Providers
An in-depth guide on Angular Dependency Providers

What is Angular? Angular is a frontend development framework used for building single-page client applications using HTML and Typescript. It is written in Typescript. What...

A simple guide to Build Angular Reactive Templates with Ngif and Async Pipe
A simple guide to Build Angular Reactive Templates with Ngif and Async Pipe

Angular Templates seamlessly help to expedite the development process with flawless assistance of components and other specific elements. These are dynamic and renders according...

A simple guide on AOT Compilation in Angular
A simple guide on AOT Compilation in Angular

What is a Compiler? A compiler is nothing but a part of code that converts one programming language to another. If we talk about some simple programming languages like C, C++,...

Our Partners

Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo

Work With Us

Well do everything we can to make our next best project!