sujith karivelil
sujith karivelil

Reputation: 29036

Relative virtual path is not allowed when using server.MapPath()

I am Trying to get the configuration file using the following code:

public void EncryptConnString()
        {
    Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Server.MapPath(@"/"));
            ConfigurationSection section = config.GetSection("connectionStrings");
            if (!section.SectionInformation.IsProtected)
            {               
                config.Save(ConfigurationSaveMode.Modified);
            }
        }

But i am getting the error

The relative virtual path 'F:/xxxx/yyyy/sample/' is not allowed here.

Note: I am accessing this code in global.asax page What i am doing wrong?

Upvotes: 1

Views: 5613

Answers (1)

Cameron Pearce
Cameron Pearce

Reputation: 98

If you pass null to this method, it will return the root config file for you:

var config = WebConfigurationManager.OpenWebConfiguration(null);

Upvotes: 3

Related Questions