Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8152047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:35:47+00:00 2026-06-06T15:35:47+00:00

How can I change the Custom error mode in web.config file programmatically? I need

  • 0

How can I change the Custom error mode in web.config file programmatically? I need to change the following:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections />

...

<system.web>
....

<customErrors mode="RemoteOnly">
   <error statusCode="404" redirect="~/errors/404" />
</customErrors>

To

<customErrors mode="off">
   <error statusCode="404" redirect="~/errors/404" />
</customErrors>

Does any one have any idea about this?

UPDATE

As CoDe aDDict answer, I try to use this:

Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
CustomErrorsSection CustomErrorsection = (CustomErrorsSection)config.GetSection("system.web/customErrors");
CustomErrorsection.Mode = CustomErrorsMode.Off;
config.Save();

But there is an exception:

Access to the path 'D:\Projects\MyProject\web.config' is denied.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:35:49+00:00Added an answer on June 6, 2026 at 3:35 pm

    I found an example some days ago which change alot in custom error configration

    public static void ConfigureCustomErrors()
        {
            Configuration config = 
            WebConfigurationManager.OpenWebConfiguration("~");
    
            CustomErrorsSection section = 
                (CustomErrorsSection)config.GetSection(
                    "system.web/customErrors");
    
            //Verify that customErrors exists in web.config
            if (section != null)
            {
                //Only configure if customErrors is enabled 
                if (section.Mode != CustomErrorsMode.Off)
                {
                    if(!section.IsReadOnly() && 
                        !section.SectionInformation.IsLocked)
                    {
                        //Collection of new redirects to add to 
                        //the customErrors element
                        CustomErrorCollection redirectsToAdd = 
                           new CustomErrorCollection();
    
                        //Page ID of the page to be used for 
                        //custom error redirects
                        int redirectPageId = 0;
    
                        //Get existing redirects, if any
                        CustomError redirect404 = 
                            section.Errors["404"];
                        CustomError redirect500 = 
                            section.Errors["500"];
    
                        //Get URL for 404 redirects
                        int.TryParse(
                            ConfigurationManager.AppSettings[
                                 "FileNotFoundPageId"], 
                                 out redirectPageId);
                        string fileNotFoundURL = 
                            ToolBox.GetSimpleAddress(
                            DataFactory.Instance.GetPage(
                            new PageReference(redirectPageId));
    
                        //Get URL for server error redirects
                        int.TryParse(
                            ConfigurationManager.AppSettings[
                            "GenericErrorPageId"], 
                            out redirectPageId);
                        string serverErrorURL = 
                            ToolBox.GetSimpleAddress(
                            DataFactory.Instance.GetPage(
                            new PageReference(redirectPageId)));
    
                        //If the 404 redirect hasn't been 
                        //specified or if its redirect 
                        //URL is invalid
                        if (fileNotFoundURL!=string.Empty && 
                           (redirect404 == null || 
                           redirect404.Redirect!=
                              fileNotFoundURL))
                        {
                            //Add new 
                            if (redirect404 == null)
                            {
                                CustomError fileNotFoundError = 
                                new CustomError(404,
                                fileNotFoundURL);
    
                                redirectsToAdd.Add(
                                   fileNotFoundError);
                            }
                            else //Modify existing
                            {
                                redirect404.Redirect = 
                                    fileNotFoundURL;
                            }
                        }
    
                        //If the 500 redirect hasn't been 
                        //specified or if its redirect 
                        //URL is invalid
                        if (fileNotFoundURL != string.Empty && 
                            (redirect500 == null || 
                            redirect500.Redirect != 
                               fileNotFoundURL))
                        {
                            //Add new 
                            if (redirect500 == null)
                            {
                                CustomError serverError = 
                                new CustomError(500, 
                                serverErrorURL);
    
                                redirectsToAdd.Add(serverError);
                            }
                            else //Modify existing redirect
                            {
                                redirect500.Redirect = 
                                    serverErrorURL;
                            }
                        }
    
                        //Add any new redirects
                        foreach (
                            CustomError redirectToAdd in 
                            redirectsToAdd)
                        {
                            section.Errors.Add(redirectToAdd);
                        }
    
                        //Save web.config if its 
                        //contents have changed
                        config.Save();
                    }
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom errors set in the web config file as follows: <customErrors mode=RemoteOnly
i can change custom color Rectangle with something like : #A125AA in xaml. But
I can change the irb prompt mode with irb --prompt prompt-mode I can see
I have a Web Application which has Global_Asax and a Custom Error Page. When
I can change SQL at runtime. Can I do the same with LINQ ?
I know I can change the target when the shadow is over it, but
In MATLAB one can change the font name of things such as ylabel ,
I know I can change a border's object with item.layer.cornerRadius = floatValue; item.layer.borderWidth =
Where I can change compiler options for C# project in VisualStudio 2008 (without command
I know I can change the selectionStyle of a UITableViewCell to make it highlight

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.