2010-06-11 21 views
13

debe ser simple, pero lo que intento vuelve nulo:Cómo leer la sección system.web de web.config

const string key = "system.web"; 

var sectionTry1 = WebConfigurationManager.GetSection(key); 

var sectionTry2 = ConfigurationManager.GetSection(key); 

estoy seguro de que he hecho esto antes.

Estoy usando MVC si esto hace la diferencia.

Respuesta

23

Estaba siendo un idiota - system.web no es una sección de configuración sino un grupo de configuración. Si cambio la clave a una sección real, ambos métodos funcionan bien. Aquí está el uso de ConfigurationManager:

const string outputCacheSettingsKey = "system.web/caching/outputCacheSettings";   

var outputCacheSettingsSection = ConfigurationManager.GetSection(outputCacheSettingsKey) as OutputCacheSettingsSection; 
+0

Para el registro, lo mismo funciona si necesita obtener la sección de compilación, excepto que la transfiere a 'CompilationSection' –

5

Creo que acceder a system.web es ligeramente diferente al acceso a las configuraciones de la aplicación.

Prueba esto:

string configPath = "/MyAppRoot"; 

Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath); 

IdentitySection section = (IdentitySection)config.GetSection("system.web/identity"); 

, es necesario especificar la sección correspondiente del system.web que está tratando de acceder a un tipo particular.

+0

En primer lugar, comprobar si 'system.web/identity' *** *** sección existe? – Kiquenet

4

Esto funcionó para mí:

public Int32 GetmaxRequestLength() 
{ 
    // Set the maximum file size for uploads in bytes. 
    var section = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection; 
    // return length converted to kbytes or return default value as specified 
    return (Int32) Math.Round((decimal)(section != null ? (double)section.MaxRequestLength * 1024/1000 : 5.120), 2); 
} 
+0

' ConfigurationManager.GetSection' dice *** machine.config *** o *** web.config ** *? Solo quiero leer *** web.config *** – Kiquenet

Cuestiones relacionadas