Within my Global.asax file, I have the following that displays a javascript alert message for application exceptions:
Response.Write("<script language='javascript' type='text/javascript'>alert('" + FixJQuotes(ex.Message) + "');</script>");
Response.Write("<script language='javascript' type='text/javascript'>history.back()</script>");
Server.ClearError();
If I access the site via http://localhost/ and generate a purpose made error (hitting a unique index in the database) the application behaves as expected, the browser shows a javascript alert and then goes back a page to show the editing page.
However, if I access the exact same server, but from a different machine, accessing on http://192.168.1.x/ hitting the same exception just results in my custom error page being displayed with no clue as to any errors
I am doing something wrong/ missing something obvious?
Thanks,
Andrew
Related
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 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
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;
%>
Can I configure ASP.NET custom errors so that it would redirect to other site when error has occurred. Even more, I would like to redirect to different web page every time.
Here is my simplified actual case:
User opens my pages with query ?urlpage=http://test.com/error.html and I would like to redirect to this page when error occurs.
How should I act in this scenario?
See http://msdn.microsoft.com/en-us/library/ed577840.aspx
And in the Page_Error method, just do:
var redirectUrl = Request.QueryString["urlpage"];
if (redirectUrl != null)
Response.Redirect(redirectUrl);
There are a number of different resources in ASP.NET that will allow you to handle errors.
I found this article on the web.config file very informative : http://articles.sitepoint.com/article/web-config-file-demystified
In addition you can setup error handling inside of the global.asax file that will trap application wide errors and allow you to send emails, log information, etc.
You can also setup page specific error handling for more custom error information - but I find that a good general purpose error handling in global.asax works for most situations.
Finally, you can go into the properties for IIS and modify where errors are redirected. For instance, set the 500 error to point to a specific file (/error.aspx or similar).
I'm working on two websites. One is an existing classic asp site which posts xml to a new asp.net (.net 3.5) website. The classic asp site is using msxml's serverxmlhttp object in vbscript to send this xml over. The whole thing works until I make a seemingly unrelated change to the asp.net site.
When I add a few lines of code that uses System.Speech.Synthesis to generate a wav file from text the classic asp websites serverxmlhttp.send command times out. As far as I can tell the asp.net page is working fine, it makes it through the few new lines of code without an issue (the wav file is generated). The few lines of speech code causing the issue is done well before the timeout.
It seems like the asp.net page was actually sending some sort of acknowledgement back to the classic page which is no longer getting sent. I should also point out that the speech code was throwing an exception saying it needed to be asynchronous which I fixed by adding Async="true" to the . However, it works when async="true", it's just those speech lines that break it. The "problem code" is just
SpeechSynthesizer speaker = new SpeechSynthesizer();
speaker.Volume = 100;
speaker.SelectVoiceByHints(System.Speech.Synthesis.VoiceGender.Female, System.Speech.Synthesis.VoiceAge.Adult, 0);
try
{
speaker.SetOutputToWaveFile("c:\\test\\output.wav");
}
catch (Exception ex)
{
retVal = false;
}
speaker.Speak(msgText);
speaker.SetOutputToDefaultAudioDevice();
Does anyone have any suggestions on what could be wrong or what I could use to help debug this?
It seems like the asp.net page was actually sending some sort of acknowledgement back to the classic page which is no longer getting sent
It sounds like you should investigate it more so you can tell us server's response behavior before and after. Also please indicate the exception thrown.
My guess would be these APIs don't work well in a service process. I have no clue though really. I'm curious about the exception, you're not clear about what you made async.