CustomError and ConnectionStrings - c#

I have an Error.aspx page to handle any Application_Error.
But there is one that I can't catch : When the site can't connect to the DB specified with the connectionstrings, Application_Error is called (in global.asax) but I don't reach Error.aspx even if there is no DB connection needed to display it. Instead, I have the standard ASP red message saying that there was an application Error on custom error page.
Any Idea to catch this error type ?

Related

ASP.NET display "site down" page instead of YSOD if failure during startup

I'm trying to serve a custom "site down" page instead of the standard YSOD if the server were ever to fail during it's startup. Using <customErrors> inside of web.Config work's fine for any server error occurring after startup has completed successfully, but in this case the YSOD response is not replaced.
Other resources have lead me to adding app_offline.htm to the root of the project and playing with its name during deployment, but this does not tackle unexpected server issues occurring during startup.
Is there a way to serve a "default" page if there are any errors or a proper HTTP response could not be formed?
Yes there is a way to do that but that depends how you want to handle those errors.
If you want to redirect to the same error page on any 4xx or 5xx status codes then you can use the below code in your web.config file
<customErrors mode="On" defaultRedirect="Error.aspx">
</customErrors>
In error page you can display some user friendly message.

Display Error Stack trace information in Custom Error page

I am using VS2010.
I log my exception in Application_Error and then get automatically redirected to Custom Error page.
On Custom Error page, I would like to display stack track of the error.
I can not implement the approach in following article because I am
using ajax controls in all the pages and I get ajax errors if I use
the solution in following article.
I updated the web.config as follows:
<customErrors defaultRedirect="~/CustomError.aspx" mode="On" redirectMode="ResponseRewrite" />
The ajax error for the web.config is as follows:
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near '
ASP.NET custom error page - Server.GetLastError() is null
I can not use the following approach because I am being told to stay
away from session and application level storage.
Asp.net 4.0 : How to get exception details in custom error page?
So what are my options?? is there any other way to store Error information in Application_Error and recover the error stack trace on Custom Error page? This page is automatically being rerouted by asp.net engine.

Showing custom error message on exception: A potentially dangerous Request.Form value was detected from the client

I am using Login Control of ASP.NET in my web application. I want to show a funny type of error on a label when this exception occures System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client
it occurs when a user try to do sql injection attack or some HTML or SCRIPT operations by entering them in Username text field of Login control. I tried many things such as enclosing the authentication login in try catch block and catching the
System.Web.HttpRequestValidationException exception also doing same for an event created by me as same as onTextChange for Username TextBox. But all these tries failed. Please tell me how to check for this exception and display a nice funny message to the attacker?
Here are a few examples of how to provide a friendlier error msg:
http://romsteady.blogspot.com/2007/06/how-to-catch-httprequestvalidationexcep.html
How to catch HttpRequestValidationException in production
The thing you need to keep in mind is that this exception is thrown before you page code happens. So you normally cannot catch the error in your page code, but only in Application_Error.
I have not tried this myself, but this site gives a alternative way to allow your page code to execute, but still protect yourself from malicious input using the built in logic, as well as catching the exception within your own code.

Errors not caught by Global.asax?

My project has an error page called oops.aspx. We have the following code in global.asax:
protected void Application_Error(object sender, EventArgs e)
{
Server.Transfer("~/oops.aspx", true);
}
oops.aspx is able to extract the error, generate a nice email to the server, and present an error message to the user.
The use of Server.Transfer preserves the URL, form information, and other helpful troubleshooting information. At one point in the oops.aspx, while composing the email, I grab Request.RawUrl and include it in the email. This is the URL (with query string parameters) of the page that caused the error.
We also have this in web.config:
<customErrors mode="On" defaultRedirect="oops.aspx"></customErrors>
When the system redirects because of an error based on this, it redirects to /oops.aspx?aspxerrorpath=/Clients/EditClient.aspx (sometimes with /, sometimes with %2f path separators). Querystring and Exception information is lost, so the email and messages generated by oops.aspx are sparse and don't say much about what was happening.
I've been getting a lot of errors of this second kind lately. The errors happen in small bunches, several of them in a few minutes, and then nothing for several hours. They happen all over the site (including on WebResource.axd and the like), which makes me think that it's not a specific error in our site but something happening at a lower level, like a Session server issue or something like that.
So, with all that, my actual question:
I'm having errors in the site that happen in such a way that they are not caught by global.asax. What can cause such errors, and how can I troubleshoot them?
Since you have enabled custom errors in your web.config, the exception will be caught before the Application_Error executes and redirect you to oops.aspx. If you don't want this from happening do not enable custom errors in your web.config. This way all errors will go through Application_Error assuming you have configured IIS to run the application pool in Integrated pipeline mode.

How to display Customised error message on website if its Database goes down

I have a ASP.NET website with SQL SERVER Database. Each time database goes down it shows a .NET error message. Is there any way to display a customised message. For eg: "Sorry, website is currently unavailable."
You would have to use try catch and handle SQL Exception and in the catch block add the customised message to your label.
Get the exception in the Global.asax and redirect them, see this.
Create and set errorpage for your website
Check Web Application Error Hanling

Categories