1 Answers
The HandleErrorAttribute allows you to use a custom page for this error. First you need to update your web.config file to allow your application to handle custom errors.
- <system.web>
- <customErrors mode=“On”>
- </system.web>
Then, your action method needs to be marked with the atttribute.
- [HandleError]
- public class HomeController: Controller
- {
- [HandleError]
- publicActionResultThrowException()
- {
- throw new ApplicationException();
- }
- }
By calling the ThrowException action, this would then redirect the user to the default error page. In our case though, we want to use a custom error page and redirect the user there instead.So, let’s create our new custom view page.
Next, we simply need to update the HandleErrorAttribute on the action method.
- [HandleError]
- public class HomeController: Controller
- {
- [HandleError(View = “CustomErrorView”)]
- publicActionResultThrowException()
- {
- throw new ApplicationException();
- }
- }