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.
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.
This mode is use for displaying error messages on a local and remote server in case of an error.
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.
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.
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 step1) First go to the file menu -> New Project create new project After that, Click on Ok
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
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.
(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.
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.
301 – This code is use to Moved Permanently
403 – This code is use for Forbidden
404 – This code is use for Not Found
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.
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.
July 29, 2021
July 22, 2021
July 20, 2021
July 16, 2021
Well do everything we can to make our next best project!
Check out our most recent blogs
July 29, 2021
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...
July 22, 2021
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++,...
July 20, 2021
Introduction Angular is a remarkable framework that may be used to create mobile and desktop web apps with spectacular UIs. It's made with JavaScript. we can utilize HTML, CSS,...
Well do everything we can to make our next best project!