【问题标题】:Setting maxRequestLength programmatically以编程方式设置 maxRequestLength
【发布时间】:2013-12-07 00:20:04
【问题描述】:

有一个名为maxRequestLength 的配置值。在配置文件中它看起来像这样:

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="2048576" />
  </system.web>
</configuration>

如何以编程方式设置maxRequestLength 的值?

【问题讨论】:

    标签: .net request


    【解决方案1】:

    你不能!

    maxRequestLength 在调用实际的HttpHandler 之前由HttpWorkerRequest 处理,这意味着在请求到达服务器并且已由相应的 asp.net 工作者。您无法控制页面代码或 HttpHandler 中的 maxRequestLength

    如果您想在代码中读取请求长度,您可以通过 HttpModuleglobal.asax 文件来实现,这就是它在 global.asax 中的实现方式:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        IServiceProvider provider = (IServiceProvider)HttpContext.Current;
        HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
    
        if (workerRequest.HasEntityBody())
        {
            long contentLength = long.Parse((workerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength)));
        }
    }
    

    如果请求长度达到所需值,您可以将web.config 中的maxRequestLength 设置为最大值,并在代码中调用worker 的CloseConnection 方法!

    【讨论】:

    • 以下似乎是阅读设置的更简单的解决方案。 stackoverflow.com/questions/4887016/…
    • @MikeSchall OP说明如何更改maxRequestLength,这是不可能的,上面的代码sn-p是如何读取任何请求的内容长度,而不是最大长度
    【解决方案2】:

    在快速谷歌之后,您似乎无法以编程方式执行此操作。 See here.

    两种可能的解决方案:

    1. 使用本地化的 web.config 配置特定目录。
    2. 使用 web.config 中的 &lt;location&gt; 元素配置特定路径。

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2018-11-26
      • 2011-04-18
      • 2012-07-18
      • 2016-02-15
      • 2014-07-26
      • 1970-01-01
      • 2011-06-06
      • 2012-02-12
      相关资源
      最近更新 更多