asp.net suppress internet explorer error dialog box - c#

This is only supposed to work on IE 8.0
On my asp.net page, I press the save button and it goes through some server side code. Upon an error on server side code, on the web page, I get Internet Explorer Error dialog titled "Errors on this webpage might cause it to work incorrectly". I know exactly what is causing this error on server side.
I do not have try catch handling in my webpages. I bubble up all my errors to Global.asax and log them in the event Application_Error.
So.. my questions are
1. How do I suppress this pop up Internet Explorer Error dialog?
2. How do I In Application_Error in Global.asax, redirect to another asp.net page called GenericErrPage.aspx where I display user friendly custom text message.
Please help. Thanks.

all I had to do was to add the following line in system.web section in web.config and it stopped the dialog box from popping up. Also, I was able to log the err in Application_Error in Global.asax file.
<customErrors defaultRedirect="~/CustomError.aspx" mode="On" />

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.

ASP.NET Core 2.2 App Service hosted on Azure returning 500 without an exception being thrown

I have developed an App Service backend hosted on Azure using ASP.NET Core 2.2.
From time to time I'm getting a 500 status but I'm not able to find out where is that 500 being generated.
I'm using application Insights and I see on Failed Requests that sometimes there are some calls to my App Service which are returning 500.
If I reproduce that same call with same values with Postman I'm not getting any 500. There are also some End Client Apps making requests to this backend. I believe it's happening to them but why I can't reproduce the same result if I'm performing the same request with same values.
I know that 500 implies "Internal Server Error" and that it is related to the Application code but I've even configured my App Service to use a Global Error handling but I never get that 500 when I'm executing the same request that I see on Application Insights.
I have set on my Configure on StartUpp class the global error handling (just to show you that I have also enabled that)
app.UseExceptionHandler(config => ExceptionHandlingSettings.ConfigureGlobalExcpetionHandling(config));
I would like to know where or how to get more information in order to find out the code generating the error/exception because I guess there is some bug in my code which throws an unhandled exception.
Here are couple of steps which you can do to understand the root cause:
1- Change stdoutLogEnabled="false" to true and then check the logs at stdoutLogFile=".\logs\stdout". The error(s) there might tell you something.
The ASP.NET Core Module stdout log often records useful error messages not found in the Application Event Log. To enable and view stdout logs:
Navigate to the Diagnose and solve problems blade in the Azure portal.
Under SELECT PROBLEM CATEGORY, select the Web App Down button.
Under Suggested Solutions > Enable Stdout Log Redirection, select the button to Open Kudu Console to edit Web.Config.
In the Kudu Diagnostic Console, open the folders to the path site > wwwroot. Scroll down to reveal the web.config file at the bottom of the list.
Click the pencil icon next to the web.config file.
Set stdoutLogEnabled to true and change the stdoutLogFile path to: \?\%home%\LogFiles\stdout.
Select Save to save the updated web.config file.
Make a request to the app.
Return to the Azure portal. Select the Advanced Tools blade in the DEVELOPMENT TOOLS area. Select the Go→ button. The Kudu console opens in a new browser tab or window.
Using the navigation bar at the top of the page, open Debug console and select CMD.
Select the LogFiles folder.
Inspect the Modified column and select the pencil icon to edit the stdout log with the latest modification date.
When the log file opens, the error is displayed.
Please make sure to remove the logging once the inspection is completed.
2 - Use Middleware for handling erroneous request like app.UseDeveloperExceptionPage();
3- You can add application Insight in your app and try to put Info logging to understand what part of your code is causing the failure.
For additional reference, please check below doc:
https://learn.microsoft.com/en-us/aspnet/core/test/troubleshoot-azure-iis?view=aspnetcore-3.0
https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-exceptions
Hope it helps.

Write and display error message for live website?

I am developing a website, At the moment when i run it off my local host every thing works fine.
I have uploaded this website to fireFTP and can now access my website online.
The website does not preform the same as when hosted on local host and i cant find the errors as my website does not break.
I would like to do some testing and am wanting to know how to display an error message??
I would like to display the error message like this:
But with a custom message like, "code stepped into method writeToSpreadSheet".
This way i could find where my code gets to and pin point whats going wrong.
You need to enable show detailed error messages in IIS settings, see the link below for a short tutorial about how to do it.
IIS7 : HOW TO enable the detailed error messages for the website while browsed from for the client browsers?
Add this lines in web.config inside so you dont have
to touch the IIS configuration:
<customErrors mode="On">
<error redirect="error404.html" statusCode="404" />
</customErrors>
Change the error404.html for your desired html page.
When some page is not found user will be redirected there

Problem with defaultRedirect in Web.Config <customErrors>

I use Asp.net 4.
Here setting for my Web.Config file.
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/ErrorPages/Oops.aspx">
<error statusCode="404" redirect="/ErrorPages/404.aspx" />
</customErrors>
I need to apply the defaultRedirect url for any Exception, and use the redirectMode="ResponseRewrite" for only Page Not Found 404.
With this setting at the moment I can manage properly the Page Not Found (I'm able to get the 404.aspx page) but if an exception arise on any other page I get the "Internet Explorer cannot display the webpage" and I'm not able to reach the defaultRedirect Oops.aspx.
What I'm doing here wrong?
One problem with having an aspx page as the target of a custom errors redirect is that errors that aren't specific to a page (e.g. errors in global.asax; errors processing web.config) will also be thrown by the target page, which can result in an infinite redirect loop.
For this reason, it's often better to have a static html page as the target of at least the defaultRedirect.
Try putting the ~ in the redirect URL.
redirect="~/ErrorPages/404.aspx"
How many bytes is the error page content and does it set an HTTP status code other than 200 OK? In Internet Explorer with "Show friendly HTTP error messages" on, which is the default, custom error pages that have content under 512 bytes in length will be suppressed by the IE browser and replaced with the "friendly" error message.
To see if this is your problem, you can try turning off the "Show friendly HTTP error messages" option under Tools, Internet Options, Advanced, Settings. It'll be under the Browsing category in the Settings area.
If the page shows up with the option turned off, try turning it back on and changing your page to add an image or some other element that will make the size over 512 bytes long.

HttpRequestValidationException showing YSOD despite customErrors section

we've modified our customErrors section to protect against the recent ASP.NET vulnerability.
Our problem is, that HttpRequestValidationException's are now causing a YSOD's to be shown, other exceptions and page not found errors are showing our custom error page.
If we change the redirectMode to ResponseRedirect everything works fine.
This is our modified section:
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error.aspx"/>
This question has nothing to do with the recent ASP.NET vulnerability!It's about the ResponseRewrite redirectMode in combination with HttpRequestValidationException's. We know there will soon be a patch and that we could change back to ResponseRedirect.
Kind regards, Martin
This happens when your error page generates an error.
Make sure you turn off request validation on your error page. <%# Page ValidateRequest="false" %>
You then of course need to encode any user input that displays on your error page. eg. Server.HtmlEncode(ex.Message)

Categories