Asked by:
Custom Error page not redirecting after used exception filter in MVC5

Question
-
User-1339639702 posted
HI Team,
I am running one MVC application where i used one layout page have all the menus. When user click on menu then it is getting redirect from javascript.
Now i used one exception filter to log the error and redirect page to custom Error page.
I have used below code, Kindly go through step by step understand if i am missing anything.
- I am using one BaseController and this controller is inherited to all the application related controller.
- Coming to the Custom error part: I used one custom Exception filter like below:
public class CustomErrorHandler:HandleErrorAttribute { #region Log Initialization FileLogService logService = new FileLogService(typeof(CustomErrorHandler)); #endregion public override void OnException(ExceptionContext filterContext) { Log(filterContext.Exception); base.OnException(filterContext); } private void Log(Exception exception) { logService.Info("==================================================================================================================" + Environment.NewLine); logService.Error(exception.InnerException != null ? exception.InnerException.ToString() : (exception.Message != null ? exception.Message.ToString() : exception.StackTrace.ToString())); logService.Info("==================================================================================================================" + Environment.NewLine); } }
Now i used this exception filter on top of Base Controller like below:
[CustomErrorHandler] public class BaseController : Controller { //Some business logic }
I used one Error Action method inside above Base Controller to return custom error page.
public ActionResult Error() { return View("~/Views/Shared/_CustomError.cshtml"); }
Now in web.config i set customError page like below:
<customErrors mode="On" defaultRedirect="/Base/Error" redirectMode="ResponseRedirect"> <error statusCode="404" redirect="/Base/Error"/> </customErrors>
Now for testing purpose one of my another controller i create one code to raise divide by zero exception.
Then while debug i found like first it is hitting the exception filter and after that process hits the Error Action method also.
But process is not redirecting to the custom error page.
Note: i used Custom Error view as .cshtml and used the same application layout inside this view, As i have to show the same layout in custom error page so that user can click on some other menu and access that report, If he found this error page.
Process will be there in the same view where this error has been occurred.
Kindly suggest.
Monday, September 16, 2019 11:06 AM
All replies
-
User753101303 posted
Hi,
According to the last line at
https://github.com/aspnet/AspNetWebStack/blob/749384689e027a2fcd29eb79a9137b94cea611a8/src/System.Web.Mvc/HandleErrorAttribute.cs
it seems coded to render a MVC view result and skip the IIS defined page.I'm not sure to get which behavior you want as it seems you are in a situation where the MVC error view is rendered already ?
Not directly related but the preferred approach is to use GlobalFilters.Filters.Add(new MyErrorAttribute()); to define globally this filter on controllers without having to inherit from a particular base controller...
Edit: ah it seems you actually want to create your own exception filter from scratch rather than to inherit from HandleErrorAttribute ? In short your intent is to just log the error and use the IIS error page ?
Monday, September 16, 2019 11:48 AM -
User-17257777 posted
Hi Vikash.Prasad,
You can set the view in “CustomErrorHandler” like this, when exceptions occur, it will go to _CustomError.schtml:
[CustomErrorHandler(View = "~/Views/Shared/_CustomError.cshtml")] public class BaseController : Controller
I make a test and it works fine:
CustomErrorHandler:
public class CustomErrorHandler : HandleErrorAttribute { //FileLogService logService = new FileLogService(typeof(CustomErrorHandler)); public override void OnException(ExceptionContext filterContext) { Log(filterContext.Exception); base.OnException(filterContext); } private void Log(Exception exception) { //your code } }
Web.config:
<customErrors mode="On" defaultRedirect="/Base/Error" redirectMode="ResponseRedirect"> <error statusCode="404" redirect="/Base/Error"/> </customErrors>
BaseController:
[CustomErrorHandler(View = "~/Views/Shared/_CustomError.cshtml")] public class BaseController : Controller { // GET: Base public void Index() { throw new NullReferenceException(); } public ActionResult Error() { return View("~/Views/Shared/_CustomError.cshtml"); } }
_CustomError.cshtml:
@{ ViewBag.Title = "_CustomError"; } <h2>_CustomError</h2> <h3>This is my error page!</h3>
Test Result:
Best Regards,
Jiadong Meng
Tuesday, September 17, 2019 9:36 AM -
User-1339639702 posted
Hi PatriceSC,
Thanks for the suggestion. I have gone through the suggested link and found it like here. I have registered my custom exception filter in filters.config file and tried but still the page is not redirecting to the error page.
Kindly suggest.
Vikash
Tuesday, September 17, 2019 10:19 AM -
User-1339639702 posted
Hi Jiadongam,
Thanks for the suggestion.
Most of the code i used already when i initiated this question. Only i added with customexception with view name.
Unfortunately, I tried this code before also. but it is not working. I wanted to make you understand like as i told above i am loading report based on clicked menu. After redirecting to the report for loading content i used jquery ajax loader once the ajax call is over then loader get hide and report content should get loaded. But if any error occurs while loading the report in that case i can see that process hits the Error action method but page did not get redirected to custom error page just loader get hide that's it.
But the screen shot you have mentioned like if manually change the url and url is not mapping to action method then page display the custom error page.
Same case is happening with me also like if i am trying to access any method which is not available then page redirect to the custom error page.
But i have to make it redirect without manually changing url.
Kindly suggest.
Vikash
Tuesday, September 17, 2019 10:39 AM -
User753101303 posted
"hits the Error action method but page did not get redirected to custom error page"
Always tell what happens. You have a MVC error view. What is confusing is that the goal for what you show is to show a MVC view when an error happens when it seems you want to show the IIS error page instead. Please make clear about what you want to happen.
It seems to me you should first drop your attribute and see if you see the error page to first make sure this part works. Then if you want to log exceptions using an attribute you should likely create one that DOES NOT INHERIT from HandleError as the purpose of this one is to show a MVC error view.
Tuesday, September 17, 2019 11:03 AM -
User-1339639702 posted
Hi PatriceSc,
As i replied to Jiadongam.
When i hit direct the Error action then i can see the Error page redirection is happening. But when error catch is happening from exception filter at that time it hits again the Error action method but Error page is not displaying, Still showing the current page.
Hope you understand.
Thanks,
Vikash
Tuesday, September 17, 2019 11:40 AM -
User-17257777 posted
Hi Vikash.Prasad,
Since you use ajax, it will not automatically redirect when error occurs on server side. One workaround is that you can define an error callback in ajax like this:
<input id="btn1" type="button" value="Click" /> <script> $("#btn1").click(function () { $.ajax({ type:"get", url: "@Url.Action("About")", success: function (result) { alert(result); }, error: function () { window.location.replace("@Url.Action("Error")"); } }) }) </script>
Test Result:
Best Regards,
Jiadong Meng
Tuesday, September 24, 2019 2:41 AM -
User-1339639702 posted
Hi Jiadongm,
Thanks for the suggestion. As i told above the scenario i used when click on menu then i get the menuUrl and use window.location to redirect to particular page. I am not using $ajax call to load the page.
Kindly suggest based on this scenario.
Thanks,
Vikash
Tuesday, September 24, 2019 10:01 AM -
User-17257777 posted
Hi Vikash.Prasad,
There is a question, when do you trigger the exception? At the time when you click the menu and use window.location to redirect to other page or when you use ajax to load the report page? To solve the issue, could you please show us the codes of the overall process of the problem, including:
- Click event handler on menu and the page it redirects to
- The related controller action
Best Regards,
Jiadong Meng
Wednesday, September 25, 2019 10:06 AM