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.
Then, your action method needs to be marked with the atttribute.
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.
- <system.web>
- <customErrors mode="On">
- </system.web>
- [HandleError]
- public class HomeController: Controller
- {
- [HandleError]
- publicActionResultThrowException()
- {
- throw new ApplicationException();
- }
- }
Next, we simply need to update the HandleErrorAttribute on the action method.
- [HandleError]
- public class HomeController: Controller
- {
- [HandleError(View = "CustomErrorView")]
- publicActionResultThrowException()
- {
- throw new ApplicationException();
- }
- }
No comments:
Post a Comment