4

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.
Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • 1
    Just for curiosity, why would you ever need that? – archil Jun 26 '12 at 12:31
  • @archil I need to change Custom error mode by get action, add an address to address bar and change configs, I know that is not a secure way but it is so helpful, also you can add some secure keys –  Jun 26 '12 at 12:42
  • In understand **what** you want to do, I just do not get **why** you need that – archil Jun 26 '12 at 13:01
  • @archil My access to the server is limited, just once at week, so I think change config programmatically, do you have any suggestion? –  Jun 26 '12 at 13:10

2 Answers2

4

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();
                }
            }
        }
    }
Raab
  • 34,778
  • 4
  • 50
  • 65
0

In order to update your web.config file, the process/identity that is running your app pool will need to have modify (write) access to the web.config file. You are getting an error because your application does not have permission to write to the web.config file. Add the proper permissions and what has been stated will work without exception. Now with that said...

DISCLAIMER

The config file drives your application, has authentication/authorization information for paths in your app, a machine key (perhaps) for encoding/decoding authentication tickets, SQL connection strings, etc. Having this be writable by your application could be a nasty security concern. I have made my disclaimer. Take caution :)

Tommy
  • 39,592
  • 10
  • 90
  • 121