Web.config Custom Errors mode Conflict - asp.net

I have a great and important problem with Web.Config, I need to see the Error of my page and resolve it in asp.net web form and web config, but when Error Occurred, I see another error and I see this Message :
customErrors mode to Off or On Or RemoteOnly,
I set this property Off, but do not show error and say again please set attribute to On your CustomError.
when I set mode to On,say Please set customErrors mode to On Again.

When you're having issues with configuration, make sure that your
settings isn't overridden.
In this case, your server might have a configuration <deployment retail="true" /> that overrides application settings and disables trace output, custom errors, and debug capabilities.
Change the value to "false"
<deployment retail="false" />
MSDN Link

Make sure you nest the customErrors tag somewhere inside your <system.web> tag like this:
<system.web>
<customErrors mode="Off" />
</system.web>
From your description it's already likely it was placed there by you or automatically generated another way. Also try setting to On and restart the Application pool. You can do this by simply modifying web.config file by adding a break return or even just hit the space key, as long as you've made a change. Now re-upload making sure this is your second time with the customErrors mode was set to On. Hopefully you can now view your custom error page.
If you are still having problems, and have access to your web site from IIS Manager try editing the .NET Error Pages in Features view. That will also do the same thing, providing your custom error page is in the correct directory for the web.config file to access.

First, you can set CustomErrors mode from "RemoteOnly" to "off"
<customErrors mode="Off"></customErrors>
Second, You can check the global.asax.maybe you create Response.Redirect as same as defaultRedirect in customError. you can check more detail in here ASP.NET customErrors with mode=remoteOnly and global.asax handling exceptions
and the last,maybe you create two system.web in your webconfig. You should only have one in your config file.please check again your webconfig.
and dont forget <compilation debug="true"/>

To show the error for debugging and NOT showing an custom error page you do not need a defaultRedirect and simply having this should then output the debug / exception information:
<system.web>
<customErrors mode="On" />
</system.web>
NOTE: Ensure that On starts with an upper-case O. By changing the web.config this should (by default) recycle your app pool and be picked up straight away.

Here is sample code how you can display exceptions on custom page.
First create Default.aspx with button:
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Throw Error" />
Add following code for button click event:
protected void Button1_Click(object sender, EventArgs e)
{
throw new Exception("Sample Exception on my Page");
}
Second create ErrorPage.aspx with label:
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
And code for error page:
protected void Page_Load(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex != null && ex.InnerException != null)
{
Label1.Text = string.Format("An error occured: {0}", ex.InnerException.Message);
}
}
And finally place following configuration in web.config:
<system.web>
<customErrors mode="On" defaultRedirect="~/ErrorPage.aspx" redirectMode="ResponseRewrite" />
</system.web>
Compile and start with Default.aspx. Click your button and error will be shown on your custom page.
Happy coding!

I just wanted to comment here, but I do not have enough point to comment, Can we see the actual, page that produces the error and see the entire web.config? you can blur out the connectionstrings and such so as not to give any passwords away.
But with what I see,
I think from what you are saying you are using IIS Express debugging on local machine.
I would done IIS Express, or ctrl+F5 the page to see if the site is just not cached.
setting the custom errors to Off will show you the debug message - NOTE the "O" in Off must be capitalized with lower case of the f's. looks like you have this, but worth mentioning
Custom Errors Off - Specifies that custom errors are disabled. This allows display of detailed errors.

I've never seen it say set to On. I've seen it say set customeErrors to off which will give you detailed errors. Set it to off and then add the actual error message you see to this post. Are you positive your not getting a different message like a 404 error?

From what I can see, you don't have the <customErrors mode="Off" /> as the first node in the <system.web> node.
If I were to guess, you have the customErrors node inside a <compilation targetFramework="x.x"> node.
You probably have the applicationpool set to another version of the .NET framework than the application is written in. Make sure the <customErrors mode="Off" /> node is the first inside the <system.web> node, then it will most likely give you the detailed errormessage.

removing this line works for me!
<customError />

Many hosting companies actually override the settings from your web.config file by theirs. You can usually set these concrete attributes in the web host's dashboard for the website.
If you tell us the concrete web hosting you are using, we should be able to assist better with concrete steps.

Set the mode to On, save the web.config then refresh or restart the IIS and make sure the mode is still set to On after saved. Browse to it again, it should show the error...

Related

ASP.NET CustomErrors not firing when exceptions occur in global.asax or in web.config

I have ASP.NET set up to use the CustomErrors functionality:
<customErrors mode="On" defaultRedirect="~/ErrorPages/500.aspx" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/ErrorPages/404.aspx" />
<error statusCode="500" redirect="~/ErrorPages/500.aspx" />
</customErrors>
Everything works nice, and the relevant error pages are shown when appropriate.
Except in the following two cases:
1) When there is an exception thrown in global.asax:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
throw new ApplicationException("Exception in Application_Start()");
}
}
2) When there is a syntax error in web.config
In both cases, I don't see my pretty 500.aspx page. Instead, I see the standard ASP.NET yellow screen of death, with the following message:
Server Error in '/MvcErrorHandling' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.
How can I get ASP.NET or IIS to show a custom error page (a pretty one, instead of the YSOD) in the above two scenarios?
Thanks in advance for any input :)
The problem is that those specific errors are happening before ASP.NET can load things up, and your "500 internal server error" page is an .aspx page that requires ASP.NET to load.
The simplest option would be to make your 500 page an HTML page, but that would mean you can't do simple error logging, etc from there.
This may still not help the web.config scenario as if IIS can't process the web.config, there's no guarantee that it would read your error section.
Another option would be to tell IIS to serve a static html page on 500 errors.
Finally you could try catching errors in the Application_Error event in the web.config - this would at least allow you to process the error, even if the page you try to display can't load up.
Edit to add
If you're running IIS 7 in integrated mode, you need to do one more thing if you're setting the response code of your error page to 500:
Response.TrySkipIisCustomErrors = true;
However, note that the following conditions will stop any custom 500 error page from displaying, resulting in the YSOD:
Errors in the web.config or certain handlers in the web.config that fire before the rest of the pipeline (i.e. the bulk of the original answer)
Errors in the error page.

404 error page not showing

I've got this in my web.config and it's being hosted by the DiscountASP.net ISP
<customErrors mode="On" defaultRedirect="">
<error statusCode="404" redirect="404.aspx"/>
<error statusCode="500" redirect="404.aspx"/>
</customErrors>
I am hosting the site on DiscountASP.net and they also tell you to config it this way. I'm using Enterprise Library but I don't think that should make a difference. I don't believe I need to config anythign for a 404 in EL.
When my page loads with an error, my 404.aspx doesn't show and I get the default custom errors off message. I do not know why I don't get my 404.aspx page showing and get this instead:
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "Off".
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's configuration tag to point to a custom error page URL.
Have you tried using a relative path to see if that makes a difference?
<error statusCode="404" redirect="~/404.aspx"/>
Alternately, try an absolute path:
<error statusCode="404" redirect="http://www.domain.com/errors/404.aspx"/>
EDIT: As others have pointed out, and based on your comment to another answer, the 404 error page should be displayed when someone navigates to a page that doesn't exist, whereas general errors on a page could be captured by the defaultRedirect. So if you're testing the 404 then make sure you're testing with a nonexistent page. To test the defaultRedirect then have one of your pages throw an exception etc.
Is it possible that you are overriding this in another web.config, say in a sub folder? Or we can go the other way. Are you sure this is in the application root?
Is there any reason why you don't want to use the defaultRedirect for 500 errors?
You are FTP-ing the files correct? Try uploading the file as a binary file instead of text. You might be suffering from an encoding problem. I'm willing to bet right now that nothing you are doing in your web.config is working.
I found a solution here.
The real catch was using this:
Response.TrySkipIisCustomErrors = true;

Freetextbox and validating requests

I am using freetextbox and have added to the web.config of my app but I still get the following error when submitting text with html:
A potentially dangerous Request.Form value was detected from the client (ctl00_MainContent_FreeTextBox1="
I know this is not the preferred way to set up an app but why am I getting these errors even though I have turned off request validation in my app?
The short answer is you shouldn't be getting such an error if you turned off Request Validation.
Did you do one of these two things correctly?
Disable on the page by inserting this at the top of the ASPX
Add the below section to your web.config.
<configuration>
<system.web>
<pages validateRequest="false" />
</system.web>
</configuration>
If that doesn't work then check the machine.config and see if the pages validaterequest value is set to true in there as that would override the web.config.
I had the same problem, and it was actually my fault. Maybe you have done the same mistake: I placed <httpRuntime requestValidationMode="2.0"/> inside
<configuration><location><system.web> instead of <configuration><system.web>.
Ensure that you haven't enabled request validation for this page. I would keep validation running for your site - but turn it off on pages where you need this control.
Be sure to sanitize anything that gets posted and be prudent about security.

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

CustomErrors mode="Off"

I get an error everytime I upload my webapp to the provider. Because of the customErrors mode, all I see is the default "Runtime error" message, instructing me to turn off customErrors to view more about the error.
Exasperated, I've set my web.config to look like this:
<?xml version="1.0"?>
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
And still, all I get is the stupid remote errors page with no useful info on it.
What else can I do to turn customErrors OFF ?!
This has been driving me insane for the past few days and couldn't get around it but have finally figured it out:
In my machine.config file I had an entry under <system.web>:
<deployment retail="true" />
This seems to override any other customError settings that you have specified in a web.config file, so setting the above entry to:
<deployment retail="false" />
now means that I can once again see the detailed error messages that I need to.
The machine.config is located at
32-bit
%windir%\Microsoft.NET\Framework\[version]\config\machine.config
64-bit
%windir%\Microsoft.NET\Framework64\[version]\config\machine.config
"Off" is case-sensitive.
Check if the "O" is in uppercase in your web.config file, I've suffered that a few times (as simple as it sounds)
In the interests of adding more situations to this question (because this is where I looked because I was having the exact same problem), here's my answer:
In my case, I cut/pasted the text from the generic error saying in effect if you want to see what's wrong, put
<system.web>
<customErrors mode="Off"/>
</system.web>
So this should have fixed it, but of course not! My problem was that there was a <system.web> node several lines above (before a compilation and authentication node), and a closing tag </system.web> a few lines below that. Once I corrected this, OK, problem solved. What I should have done is copy/pasted only this line:
<customErrors mode="Off"/>
This is from the annals of Stupid Things I Keep Doing Over and Over Again, in the chapter entitled "Copy and Paste Your Way to Destruction".
For Sharepoint 2010 applications, you should also edit C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\web.config and define <customErrors mode="Off" />
I tried most of the stuff described here. I was using VWD and the default web.config file contained:
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
I changed mode="RemoteOnly" to mode="Off". Still no joy.
I then used IIS manager, properties, ASP.Net Tab, Edit configuration, then chose the CustomeErrors tab. This still showed RemoteOnly. I changed this to Off and finally I could see the detailed error messages.
When I inspected the web.config I saw that there were two CustomErrors nodes in the system.web; and I have just noticed that the second entry (the one I was changing was inside a comment). So try not to use notepad to inspect web.config on a remote server.
However, if you use the IIS edit configuration stuff it will complain about errors in the web.config. Then you can rule out all of the answers that say "is there an XML syntax error in your web.config"
The one answer that actually worked to fix this I found here: https://stackoverflow.com/a/18938991/550975
Just add this to your web.config:
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
</system.webServer>
<configuration>
You can generally find more information regarding the error in the Event Viewer, if you have access to it. Your provider may also have prevented custom errors from being displayed at all, by either overriding it in their machine.config, or setting the retail attribute to true (http://msdn.microsoft.com/en-us/library/ms228298(VS.80).aspx).
My problem was that i had this defined in my web.config
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<remove statusCode="500" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error/Internal" />
</httpErrors>
I also had this problem, but when using Apache and mod_mono. For anyone else in that situation, you need to restart Apache after changing web.config to force the new version to be read.
If you're still getting that page, it's likely that it's blowing up before getting past the Web.Config
Make sure that ASP.Net has permissions it needs to things like the .Net Framework folders, the IIS Metabase, etc. Do you have any way of checking that ASP.Net is installed correctly and associated in IIS correctly?
Edit: After Greg's comment it occured to me I assumed that what you posted was your entire very minimal web.config, is there more to it? If so can you post the entire web.config?
Actually, what I figured out while hosting my web app is the the code you developed on your local Machine is of higher version than the hosting company offers you. If you have admin privileges you may be able to change the Microsoft ASP.NET version support under web hosting setting
We had this issue and it was due to the IIS user not having access to the machine config on the web server.
We also ran into this error and in our case it was because the application pool user did not have permissions to the web.config file anymore. The reason it lost its permissions (everything was fine before) was because we had a backup of the site in a rar file and I dragged a backup version of the web.config from the rar into the site. This seems to have removed all permissions to the web.config file except for me, the logged on user.
It took us a while to figure this out because I repeatedly checked permissions on the folder level, but never on the file level.
I had the same issue but found resolve in a different way.
-
What I did was, I opened Advanced Settings for the Application Pool in IIS Manager.
There I set Enable 32-Bit Applications to True.
Try restarting the application (creating an app_offline.htm than deleting it will do) and if you still get the same error message, make sure you've only declared customErrors once in the web.config, or anything like that. Errors in the web.config can have some weird impact on the application.
Do you have any special character like æøå in your web.config? If so make sure that the encoding is set to utf-8.
Is this web app set below any other apps in a website's directory tree? Check any parent web.config files for other settings, if any. Also, make your your directory is set as an application directory in IIS.
If you're using the MVC preview 4, you could be experiencing this because you're using the HandleErrorAttribute. The behavior changed in 5 so that it doesn't handle exceptions if you turn off custom errors.
You can also try bringing up the website in a browser on the server machine. I don't do a lot of ASP.NET development, but I remember the custom errors thing has a setting for only displaying full error text on the server, as a security measure.
I have just dealt with similar issue. In my case the default site asp.net version was 1.1 while i was trying to start up a 2.0 web app. The error was pretty trivial, but it was not immediately clear why the custom errors would not go away, and runtime never wrote to event log. Obvious fix was to match the version in Asp.Net tab of IIS.
Also make sure you're editing web.config and not website.config, as I was doing.
I have had the same problem, and the cause was that IIS was running ASP.NET 1.1, and the site required .NET 2.0.
The error message did nothing but throw me off track for several hours.
Make sure you add
right after the system.web
I put it toward the end of the node and didn't work.
If you are doing a config transform, you may also need to remove the following line from the relevant web.config file.
<compilation xdt:Transform="RemoveAttributes(debug)" />
Having tried all the answers here, it turned out that my Application_Error method had this:
Server.ClearError();
Response.Redirect("/Home/Error");
Removing these lines and setting fixed the problem. (The client still got redirected to the error page with customErrors="On").
I have had the same problem, and I went through the Event viewer application log where it clearly mention due to which exception this is happened. In my case exception was as below...
Exception information :
Exception type: HttpException
Exception message: The target principal name is incorrect. Cannot generate SSPI context.
at System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app)
at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers)
at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context)
at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context)
at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)
The target principal name is incorrect. Cannot generate SSPI context.
I have just updated my password in application pool and it works for me.
It's also possible in some cases that web.config is not formatted correctly. In that case you have to go through it line by line before will work. Often, rewrite rules are the culprit here.
That's really strange. I got this error and after rebooting of my server it disappeared.
For me it was an error higher up in the web.config above the system.web.
the file blah didn't exist so it was throwing an error at that point. Because it hadn't yet got to the System.Web section yet it was using the server default setting for CUstomErrors (On)
None of those above solutions work for me. my case is
i have this in my web.config
<log4net debug="true">
either remove all those or go and read errors logs in your application folder\logs
eg.. C:\Users\YourName\source\repos\YourProjectFolder\logs

Resources