tag net mvc component asp asp.net asp.net-mvc-3 iis-7 web-config configurationmanager

component - Cómo leer el valor del sistema desde web.config y usarlo en el método ASP.NET MVC C#



partial views asp net mvc (3)

Parece que no hay una manera fácil de leer la sección system.webServer, porque está marcada como "ignorada" desde machine.config.

Una forma es analizar el XML del archivo web.config directamente:

var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); var section = config.GetSection("system.webServer"); var xml = section.SectionInformation.GetRawXml(); var doc = XDocument.Parse(xml); var element = doc.Root.Element("security").Element("requestFiltering").Element("requestLimits"); string value = element.Attribute("maxAllowedContentLength").Value;

Estoy trabajando en una aplicación web ASP.NET MVC3 (no escrita por mí) que tiene un tamaño máximo de carga de 100MB. Ahora esta aplicación web se instala en las máquinas servidoras para los clientes, por lo que sería bueno que este tamaño máximo de carga se pudiera configurar para cada cliente. Ellos tienen acceso para editar el web.config para la aplicación web si es necesario.

Ahora hay un valor en el web.config así:

<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="104857600" /> </requestFiltering> </security> </system.webServer>

También hay otro valor aquí que parece ser similar:

<system.web> <httpRuntime maxRequestLength="104857600" executionTimeout="360" /> </system.web>

Ese 104857600 bytes parece ser el límite de carga de archivos de 100 MB. Sin embargo, al cambiar el valor, descubrí que este no es el valor autorizado y que no estaba obedeciendo el nuevo límite. Así que después de excavar un poco más encontré en otro lugar en el código C # un valor codificado public const double MaxContentSize = 104857600 y otro método C # estaba usando ese valor para aceptar / denegar la carga del archivo Ajax.

Entonces, lo que creo que me gustaría hacer es reemplazar ese número codificado en el código para que lea el valor en el web.config. Entonces, al menos, cualquiera puede cambiar ese valor en web.config cuando implementa el sitio web.

¿Puedes hacer algo como esto?

MaxContentSize = ConfigurationManager.systemWeb.httpRuntime[''maxRequestLength''];

He visto algunos ejemplos usando appSettings en el web.config, por ej.

<appSettings><add key="MySetting" value="104857600" /></appSettings>

luego accediéndolo como:

ConfigurationManager.AppSettings["MySetting"]

Pero eso significaría agregar un valor personalizado allí y ahora tendríamos 3 lugares para cambiarlo en el web.config. Alguien tiene una idea de cómo hacerlo correctamente?

Muchas gracias


Puedes hacer algo como:

int maxRequestLength = 0; HttpRuntimeSection section = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection; if (section != null) maxRequestLength = section.MaxRequestLength;


Tratar:

var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/") var section = (System.Web.Configuration.SystemWebSectionGroup)config.GetSectionGroup("system.web") var maxRequestLength = section.HttpRuntime.MaxRequestLength