繁体   English   中英

如何阅读maxAllowedContentLength

[英]How to read maxAllowedContentLength

对于我的网络应用程序,我有用于上传文件的flash组件。 我想在客户端处理最大文件大小限制而不实际将该文件发送到服务器。 所以我需要以某种方式从配置文件中读取该值以将其发送到客户端。 我发现的一些文章说直接读取配置文件不是解决方案,因为它可以在很多地方进行更改。 所以应该有一些API调用,但我找不到任何...

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

我知道这是一个古老的问题,但是我花了很多时间(浪费了),我觉得为那些可能会进入我道路的人发布一个有效的解决方案:

Using Microsoft.Web.Administration;

uint uiMaxAllowedContentLength = 0;
using (ServerManager serverManager = new ServerManager())
{
    Configuration config = serverManager.GetWebConfiguration("Default Web Site/{{your special site}}");
    ConfigurationSection requestFilteringSection = config.GetSection("system.webServer/security/requestFiltering");
    ConfigurationElement requestLimitsElement = requestFilteringSection.GetChildElement("requestLimits");
    object maxAllowedContentLength = requestLimitsElement.GetAttributeValue("maxAllowedContentLength");
    if (null != maxAllowedContentLength)
    {
        uint.TryParse(maxAllowedContentLength.ToString(), out uiMaxAllowedContentLength);
    }

}

确保首先下载并安装Microsoft Web Administration包(

PM>安装包Microsoft.Web.Administration

此外,您可能需要调整web.config文件的权限。 给IUSR和IIS_IUSRS至少“读取”权限。

该代码实际上来自微软网站,但发现它需要永远! 希望我已经为你节省了几个小时。

干杯,

罗马

没有Microsoft Web管理包:

using System.Web.Configuration;
using System.Configuration;
using System.Xml;
using System.IO;

Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
IgnoreSection ignoreSection = configuration.GetSection("system.webServer") as IgnoreSection;
string sectionXml = ignoreSection.SectionInformation.GetRawXml();
StringReader stringReader = new StringReader(sectionXml);
XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
UInt32 maxAllowedContentLength = 0;
if(xmlTextReader.ReadToDescendant("requestLimits"))
    UInt32.TryParse(xmlTextReader.GetAttribute("maxAllowedContentLength"), out maxAllowedContentLength);

试试这种方式

您可以根据配置文件中的Web配置更改以下代码段

我的web.config有些看起来像

<system.web>
  <httpRuntime executionTimeout="30"  maxRequestLength="100"/>

在这里你可以看到maxRequestLength定义为100,可以从页面后面的代码更改

using System.Web.Configuration;添加using System.Web.Configuration;

现在编写此代码以更改maxRequestLength的值

Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
HttpRuntimeSection httpruntinesec =
    (HttpRuntimeSection)configuration.GetSection("system.web/httpRuntime");

您可以使用httpruntinesce实例设置该值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM