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.
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 ?
[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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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.
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.
"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.
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.
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:
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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.
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
21 Posts
Custom Error page not redirecting after used exception filter in MVC5
Sep 16, 2019 11:06 AM|Vikash.Prasad|LINK
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.
Now i used this exception filter on top of Base Controller like below:
I used one Error Action method inside above Base Controller to return custom error page.
Now in web.config i set customError page like below:
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.
All-Star
48520 Points
18073 Posts
Re: Custom Error page not redirecting after used exception filter in MVC5
Sep 16, 2019 11:48 AM|PatriceSc|LINK
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 ?
Participant
1320 Points
491 Posts
Re: Custom Error page not redirecting after used exception filter in MVC5
Sep 17, 2019 09:36 AM|jiadongm|LINK
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:
Web.config:
BaseController:
_CustomError.cshtml:
Test Result:
Best Regards,
Jiadong Meng
None
0 Points
21 Posts
Re: Custom Error page not redirecting after used exception filter in MVC5
Sep 17, 2019 10:19 AM|Vikash.Prasad|LINK
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
None
0 Points
21 Posts
Re: Custom Error page not redirecting after used exception filter in MVC5
Sep 17, 2019 10:39 AM|Vikash.Prasad|LINK
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
All-Star
48520 Points
18073 Posts
Re: Custom Error page not redirecting after used exception filter in MVC5
Sep 17, 2019 11:03 AM|PatriceSc|LINK
"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.
None
0 Points
21 Posts
Re: Custom Error page not redirecting after used exception filter in MVC5
Sep 17, 2019 11:40 AM|Vikash.Prasad|LINK
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
Participant
1320 Points
491 Posts
Re: Custom Error page not redirecting after used exception filter in MVC5
Sep 24, 2019 02:41 AM|jiadongm|LINK
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
None
0 Points
21 Posts
Re: Custom Error page not redirecting after used exception filter in MVC5
Sep 24, 2019 10:01 AM|Vikash.Prasad|LINK
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
Participant
1320 Points
491 Posts
Re: Custom Error page not redirecting after used exception filter in MVC5
Sep 25, 2019 10:06 AM|jiadongm|LINK
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:
Best Regards,
Jiadong Meng