Set specific error pages - asp.net

In my MVC project i set a Default error page in the web.config
<customErrors defaultRedirect="~/Error/Index" mode="RemoteOnly">
</customErrors>
till now all is fine, in any exception thrown the user get the default error page.
Now i want to add another error page that in specific exception will load instead of the default error page that set in the web.config.
I had an idea: maybe in a new class that inherit "ActionFilterAttribute" and implement "IExceptionFilter", i will in any exception thrown from an action if the type of the exception is my specific case, in this case i need to change the redirect route that set in the web.config as default, but how can i do it? or maybe someone have an another idea?

You don't need to change anything in the web.config. The customErrors in the catches only the exceptions you didn't catch in yours ActionFilters and Controllers.
Good plan Boss!

And read this SO Answer about how I do it, etc. It might help :)

You can specify what views are rendered for a given exception with the HandleErrorAttribute filter

Related

asp.net mvc errorhandler not showing custom error page

I am trying to follow the examples in this link and this one but instead of showing the error page I get an HTTP 500 Internal server error.
I have the <customErrors mode="On" /> set in the webconfig. I have even tried applying the [HandleError] filter to the controller class. I tried without as well. The Error.aspx is present in /Views/Shared/ as well so it couldn't be a case of no file found.
I threw a DivideByZero exception in my controller's action method. I want to follow that example so that I can specify a separate error page for all the actions that need them.
I am using the aspx view engine in a blank project that I created. That should not be the reason for it right?. I am also using a master page for this. Anything that I could be missing?
Thanks
Edit-Added Code
I added this code to a new project and made the web.config entry <customErrors mode="On" />
[HandleError]
public class HomeController : Controller
{
//
// GET: /Home/
[HandleError]
public ActionResult Index()
{
throw new DivideByZeroException();
return View();
}
}
It didnt work! Then I commented that DivideByZeroException and in the aspx view just added and invalid Model.Property. In both instances I got the Internal server error. I have done everything there is to be done as per documentation. I see a lot of other people having the same problem and not being able to solve it too. Is this a bug in MVC 3?
I think I have figured out what really was the problem. My main Home page index was in a master page. I added the same master page to the default error page that comes out of the box with Visual Studio and it worked for me.
The documentation needs to point out more clearly this important requirement,... if it indeed is one.
Another amazing revelation is that you don't need to decorate your controller classes with the [HandleError] attribute. It works without that as well for the default Error view. You may provide that attribute if you want a specific custom view for your action or controller.
Like so:
[HandleError(View = "CustomError")]
...where CustomError.aspx is just another plain aspx view page in either the shared folder for the View or in the View-Controller folder itself.
Has anyone got this working without putting the error pages in a master page, where the main calling page that throws the error is in a master page?
The HandleError filter doesn't catch all the errors. It doesn't catch exceptions that are raised outside controller actions/action filters. Also, it doesn't catch HTTP exceptions having status code other than 500.
So you have to make sure where the exception is getting thrown and you should not rely only on the HandleError to return the custom error page but you also have to set a custom error page in the customErrors section as well.
<customErrors defaultRedirect="error.htm" mode="On"
redirectMode="ResponseRewrite" />
Make sure you have activated custom errors in your web.config:
<customErrors mode="On" />
Also make sure that the ~/Views/Shared/Error.aspx template is present because this is what will be rendered in case of error.

IIS error page is preventing me finding an error in ASP.NET Web API

I'm using ASP.NET web API. At some point an exception is being thrown before the action is being executed (possibly during DI for the controller), but I can't track it down because I just get a canned IIS error page. I have no idea how to get the Exception information.
How can I get a response with some useful information?
EDIT: I have verified that a custom ExceptionFilter will respond to exceptions thrown by a controller, but not to this exception which I believe is occurring before any controller code executes. Adding code to Application_Error() in Global.asax also doesn't respond to the Exception. The framework appears to be blind to this Exception. So that's nice.
Check the windows event log on the server in the Application log.
Do you know how, and have you configured (or turned off) custom errors?
have you tried adding this
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
under the configuration node in your config file. it will inform IIS7.5 to ignore the IIS settings and pass the error along to the site.

HttpHandler for 404 error stopped working after 4.0 upgrade

I have an HttpHandler which takes care of 404 errors, which I implemented years ago as a way of doing url routing. If the request maps to a valid page, it redirects to that page. Otherwise, it returns a 404. (I know I should start using the new routing feature of asp.net 4.0, but that will take some time. I need to get this working asap.)
In IIS6, I have mapped the 404 error to "/404.ashx". In the web.config, custom errors are set up like so:
<customErrors mode="On" defaultRedirect="error.aspx">
<error statusCode="404" redirect="/404.ashx"/>
</customErrors>
and the http handler:
<httpHandlers>
<add verb="GET" path="404.ashx" type="myNamespace.PageNotFoundHandler,myAssemblyName"/>
</httpHandlers>
This has been working for years - it stopped working as soon as I changed the site to use asp.net 4.0. Everything was recompiled for 4.0, and there were no code changes.
Now, when I hit one of these urls that used to work, I get a blank page with a 404 error code. If I remove the IIS 404 error mapping, I get a regular old 404 page. It seems that the httphandler is not being called.
I have other http handler which are working fine.
I have set the EnableExtensionlessUrls item in the registry to 0, as suggested here and other places.
I figure there must be some configuration setting I missed or something like that. Naturally, this is a problem only on my production server, so I can't run it in the debugger to see what is happening. If nothing easy comes up, I will insert some extra logging in my system to help track it down.
Any ideas on what I else can check?
You might check to see if it's the Verb attribute, maybe it should be POST or *?
Another quick fix would be to add code to your Application_Error event handler in your global class like so.
for (Ex = Server.GetLastError(); Ex != null; Ex = Ex.InnerException)
{
if (Ex is HttpException && ((HttpException)Ex).GetHttpCode() == 404 && Context != null)
{
Context.Server.Transfer("~/404.ashx");
return;
}
}
This would remove the need for any configuration settings inside of your web config, but it might behave somewhat differently than what you currently use as it would take on the page identity of the currently requested page/file instead of passing along the path in a query string as the normal IIS 404 custom error page is handled.
Changing from the default app pool to the classic one fixed it for me.

respond with 404 error from asp.net page codebehind

I have a scenario in which I'm serving a file from codebehind.
which file, depends on request. in some cases there will be no file to serve and I want to return 404 to the browser.
how can I do that from codebehind? is this the correct course of action to show user there's no file available?
you can use the Response.StatusCode property to return a 404:
Page.Response.StatusCode = 404
As for the question of whether it's the "correct thing to do" I'd say it depends how the Page is going to be accessed. If you're going to access it programmatically then yes I'd go with the 404. If however it is going to be a user facing system, then I'd go with a custom page of some sort. Programs like codes and humans like more understandable things :-)
throw new HttpException(404, "File not found");
I'd be more inclined to redirect them to a custom error page that clearly indicates that the file cannot be found, in the style of the rest of your web application.
You can specify how to handle certain errors in web.config
<customErrors mode="On">
<error statusCode="404" redirect="FileNotFound.aspx"/>
</customErrors>

Possible to use customErrors=On, but only for non-IIS handled errors?

I toggled customErrors=On in web.config, set the defaultRedirect for my custom Error.aspx page. However, this is only to prevent application errors from spitting out code - I'd still like IIS to handle 404s etc. with its default handlers, since they work fine.
Any way to specify in my ASP.NET app or IIS for IIS to take priority?
I know I could add the formatting in my Error page...but I'd just be replicating what exists in IIS.
It didn't tried it (I don't have visual studio on that box) but maybe if you set your customErrors to handle only error 500 (server error), that'll work. Just a guess.
Let me know if it works
You can do it in the web.config file
<customErrors = "On">
<error statusCode = "404" redirect = "route_name"/>
</customErrors>
Don't forget to register the route in the Global.asax.cs

Resources