I have a page that reveives a lot of incoming traffic. Some of these fail, and i want to redirect them to my main page to avoid losing potentional customers. I have tried this in my global.asax
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError();
if (ex is HttpException)
{
if (((HttpException)(ex)).GetHttpCode() == 404)
Server.Transfer("~/Default.aspx");
}
// Code that runs when an unhandled error occurs
Server.Transfer("~/Default.aspx");
}
And i tried using custom errors, but my application keeps giving me the IIS 7.5 HTTP 404 error page, even though i should have handled it myself.... Everything is working as intended in my development environment, but on my hosted solution is isnt working... any solutions?
HTTP 404 is not application error. It is client error which means that requested web resource is not found / doesn't exist. It is returned by web server before it ever reach your application so your error code will never be executed.
Edit: Actually I'm not sure if this didn't change in IIS 7.x integrated mode but for IIS 6 and IIS 7.x in classic mode above statement is true.
Try the following:
throw new HttpException(404, "Not Found");
This should redirect user to the custom error page defined in web.config.
Also, it is usually considered as bad practice to redirect user to the main page instead of showing special "Not found" page. User should be aware of the error. Better way is to offer user some useful links on error page as well as quick search form.
I see in this code a potential dead loop on the same page (default.aspx).
Solved using a http handler and registering it in web.onfig
Related
In the site settings of our DotNetNuke installation, we set the Default Page for "500 Error Page" as you can see below.
After setting this, we were expecting to be redirected when an error occurs. Instead we're still redirected the the "Default.aspx?tabid=..." page.
Why isn't the correct page shown?
What do we need to change for it to work?
(We're using v9.02.00 366, .NET Framework 4.6)
EDIT: Here's how I force the error to occur using a custom module.
public partial class TriggerError500 : PortalModuleBase
{
protected void Page_Load(object sender, EventArgs e)
{
if(UserId == -1)
{
throw new NotImplementedException();
}
}
}
This module has been placed on a public page to test the error 500 page.
The 500 error page will most likely only be used when an exception is completely unhandled. For example, if an exception is handled by a developer, then a friendly message will be shown on the page with part of the exception in the URL. This may account for the URL thing you're seeing. It's the same page as the module in question, but in a different format.
When the exception is not handled, a visitor would ordinarily be shown the infamous "yellow screen of death" (YSOD) with error details. Depending on the settings in the web.config, the level of detail will be generic or detailed. I believe that this is the use case intended for the 500 error page. This is when you should see it.
If my memory serves me right, you may want to try this in your web.config:
<customErrors mode="On" defaultRedirect="500" />
A setting that will affect error handling at a platform level can be toggled in the security section
Screen Shot here
Due to the way that DNN handles the loading of modules, and pages, the code that you are writing does not actually trigger an HTTP 500 error, as the page itself is loaded properly. The module loading error is captured by the framework and the error is logged to the Admin Logs, but the page itself is rendered.
You typically get an HTTP 500 error when you cannot connect to the database or otherwise, in those cases the DNN will adhere to the rules.
It is possible, that you could set Response.StatusCode = 500; and then end the response and get the desired behavior, but i have NOT tested this.
I have a global error handling for the controller which is working fine. But sometimes when any web.config element are missing or can't able to load it shows the error screen and hitting the application-error in the global.asax as the the exception never reaches the pipe line. Also I was able to redirect to a custom error page by setting custom error as remoteonly with redirection page but not able to log it as not hitting application-error in global.asax. Does anyone know how to handle the exception here
I guess, Exception handles your code only, the exception which you were taking is before any code to be execute and it will handle by Web Server i.e. IIS
I am having a huge website with over 100 single sites in ASP.Net.
Of course I try to catch every action with a try-catch block or with other things like validation-controls.
But I want, IF a error happens which I get not catched to happens following:
1) Write the Error in Database
2) Show user a specific site instead the errorsite from asp.net
How to do that?
You can use the Application_Error event handler in in Global.asx to handle any exceptions that are not caught at the page level. See, for example,
http://msdn.microsoft.com/en-us/library/24395wz3(v=vs.100).aspx
It's kind of up to you what you do in the event handler, so you can log the error to a database if you want to. You can also redirect to another page of your choosing to display the error however you want.
Note that the Application_Error event will be raised for all uncaught exceptions, including Http exceptions (e.g. 404 Not found). You probably don't want to log those.
You should try ELMAH
Check out the blog post of Scott Hanselman on how to integrate it in asp.net website and make it work.
Below is the article from Scott Mitchel on how to log errors with Elmah and how to show custom error page to user:
http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/logging-error-details-with-elmah-cs
http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs
If you not into trying elmah, which is a breeze to setup. It depends on how your 100 sites are setup. But you possibly could look into using the global
Application_Error event in global.asax.cs and add you own handling code in there.
protected void Application_Error(object sender, EventArgs e)
{
var lastException = Server.GetLastError();
//log it to db, re-route the request to an alternate location ... etc
}
Another option, again it depends on how your sites are setup/hosted would be to read the event_log on the server, and check for ASP.NET errors saving the relevant details to the db.
Code snippet..
if (regionalApprover == null)
{
throw new Exception(string.Format("The regional approver for {0} could not be found", companyData["Country"]));
}
How does the user actually see this error ?
The result of an unhandled exception depends on a variety of factors, including
where the web request is coming from,
the settings of the <customErrors> Element in your web.config and
the contents of Application_Error in your global.asax codebehind file.
In the default configuration, IIS will log the error into the Windows event log. In addition, it is shown in the browser by ASP.NET if the web request comes from localhost.
If you're trying to display an error message on the page (that the user is supposed to see), don't use Exceptions.
It's a much better idea to add an errors section to the page that you can add the messages to before showing the page to the user.
I want to simulate this error so I can check a generic error page is displayed, not the HTTP 500 one, in light of the recent security vulnerability.
We include special processing in the site itself for 404 and 403 so I want to make sure that errors without special processing work too.
throw new Exception();
This will generate a HTTP 500
I think you can do this by overriding page init and adding the 500 status code to the response like the following:
protected void Page_Init(object sender, EventArgs e)
{
Response.Clear();
Response.StatusCode = 500;
Response.End();
}
Enjoy!
Here's a way to do this without modifying your site in any way:
From your web browser, open a page on your site that has a postback form.
Press F12 to open developer tools.
From the HTML tab, search for __VIEWSTATE and change the value in any way.
Post to the form
This will cause a "Validation of viewstate MAC failed" ASP.Net Exception, which returns a 500 internal server error HTTP response code.
Breaking the web.config with a malformed tag also works, but defeats the purpose if you are trying to test some settings in your web.config (like Failed Request Tracing).
Change the name of your dll file. It will crash the app if you ask for a route afterwards because it won't find the controller. I used this to test my logging.
you can break the web.config file. Put a malformed tag for tests
This generate a custom http error code in classic asp.
<%#language=Jscript%>
<%
Response.Status = "996 TerraNova GeoWeb Internal Server Error";
Response.End;
%>