MVC default redirect Error page doesn't always show - asp.net-mvc-3

I have a custom error page in my MVC application that's just ~/error/ but when I turn Custom Errors on in the Web.Config like so:
<customErrors mode="On" defaultRedirect="~/error/" />
It only works for 400 errors not server-side 500 errors and instead gives me the following error message on a white page:
"Sorry, an error occurred while processing your request."
How can I just make every single error go to the defaultRedirect page?

500 errors should be handled by the MVC application itself. Custom Errors defined by the web.config are for errors other than 500 errors.
Basically, you need to use the RegisterGlobalFilter method (in global.asax) to add a new HandleErrorAttribute to the filter collection (you'll name the view to use for errors in the HandleErrorAttribute), then create a view that takes as its model System.Web.Mvc.HandleErrorInfo. There is no controller that handles these errors or sends the model to the view, so you can't just redirect to your error page.
You can get more information at http://community.codesmithtools.com/CodeSmith_Community/b/tdupont/archive/2011/03/01/error-handling-and-customerrors-and-mvc3-oh-my.aspx.
EDIT There is an alternative method, where all errors are handled through MVC, shown in How do I display custom error pages in Asp.Net Mvc 3?. Basically, you set up a protected void Application_Error()in your global.asax file to handle all errors, without going through web.config.

Related

Handle redirect function when ASP.NET Core Web MVC error in application startup ( program.cs )

I want to handle my net core website able to catch the error and redirect to the error page or other sites if the middleware error or web application build fails at startup.
Methods I have tried
1.Add custom ErrorHandlerMiddleware and redirect page in trycatch,but redirect cause infinite loop and getting error "ERR_TOO_MANY_REDIRECTS"
2.Use "UseExceptionHandler" and errorHandlingPath to Error Controller return and redirect to target page,but getting http error 500
Is it possible to handle redirect to other pages when net core web startup error?
If web application build failed,You may not be able to reach the Error Page
If you want to handle the errors may cause webapplication build failure,you'd better directicty write the response with lambda
app.UseExceptionHandler(x => x.Run(async context=> {
context.Response.StatusCode = 500;
await context.Response.WriteAsync("SomeError");
}));

Laravel Exceptions handling when debug=false

I use the default Laravel Exception handler, but I have a question regarding good practices.
When I have a condition in which the controller must throw an exception, I do the following:
if($blah) {
abort(500, "The user should not be here");
}
and then on the frontend, I have a view that handles the error as this:
#section('message', __($exception->getMessage() ?: 'Whoops, something went wrong on our servers.'))
So that works great as long as I have the following on the .env file:
APP_DEBUG=true
Whenever I change this value to false, (as it should in production), all I get on the error page is: Server Error instead of my custom message.
How can I keep my custom error message, and have APP_DEBUG=false in production?
If you want to customize you HTTP errors for specific views or conditions, then you will have to look into Rendering exceptions section that will show you how to show a custom view for a specific exception. With this you can throw an exception like
throw new UserNotFoundException();
and listen to that exception and render a specific view as required.

ajax error on current page when redirecting to another page in jsf application

I'm using the following code to redirect to another page,
<p:commandButton value="myRedirectButton" value="#{myBean.val}" rendered="#{myBean.renderThis}"
onclick="remoteRedirect();"/>
<p:remoteCommand name="remoteRedirect" actionListener="#{myBean.redirectToPage}"/>
And the actionListener in java is as follows:
public static void redirectToPage(String url){
FacesContext ctx= FacesContext.getCurrentInstance();
ExternalContext ext=ctx.getExternalContext();
String encodedUrl=ext.encodeRedirectUrl(ctx.getApplication().
getViewHandler.getActionUrl(ctx,url),
new HashMap<String,List<String>>());
ext.redirect(encodedUrl);
}
On clicking the 'myRedirectButton', a js error pops up just before the page redirects saying
"This message is only sent, because project stage is development and no other error listeners are registered".
Does anyone have any ideas on why this is happening?
This message is only sent, because project stage is development and no other error listeners are registered
This will show up when you're using MyFaces, and you have javax.faces.PROJECT_STAGE set to Development in web.xml, and you don't have any jsf.ajax.addOnError listener, and the current request is an Ajax request, and the JS code has thrown an error during that request.
Basically, it's just trying to tell you that you should look in the JS console for the actual error, and/or that you should configure a jsf.ajax.addOnError for more fine grained JS error handling. Once having the detail about the actual JS error, you can easily naildown the cause and solve the problem.
As to the concrete problem, you're basically sending multiple ajax requests at the same time on click of the <p:commandButton>, first one via <p:remoteCommand> and immediately thereafter second one via <p:commandButton>. The first one will perform a redirect and hereby unexpectedly abort the second one before its response can be processed, hereby causing a JS error.
There are basically 2 ways to solve your timing problem:
Move the action from the <p:remoteCommand> to the <p:commandButton> and then remove the onclick and <p:remoteCommand> altogether.
Add a return false; in the end of the onclick to block the <p:commandButton> from sending its own ajax request.

Call to PageMethods in WebForm returns 404 in MVC project

I am trying to add a WebForm to an MVC project to see if the code can be reused. Inside the WebForm, the JavaScript call to PageMethods returns failure:
The server method '{my pagemethod}' failed
There is no stack trace, the HTTP status code is 404 and the status text is null.
The page was working in the WebForm project and I confirmed that the parameters were passed correctly to the PageMethods call.
Is there something to expose the PageMethods of WebForm in an MVC project?
The HTTP 404 is caused by the page not being found.
Adding the following line to the RouteConfig solved the issue:
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");

MVC LazyInitializer Error Handling

I want to have an error Page if My Aplication cannot connect to Database.
But for now I have sth like this:
Then If I step over I have an error in Layout where I check if IsAuthenticated it also shows an error in
<li>#if (WebSecurity.IsAuthenticated)
{
if (User.IsInRole("admin"))
{#Html.ActionLink("Admin Panel", "Index", "Admin")}
But it only happens if I am logged in.
If I am not logged in then it only shows an error which I showed in Picture,
Then if I click step over I get an error page in web browser which is a result I want to have.
Q: How to get an error Page if Database is not connected without going to VS10 to code view ?
I also have an error controller and it works for displaying error page if someone types wrong www address.

Resources