设置了maxRequestLength编程maxRequestLength

2023-09-04 02:45:10 作者:L’amour gardez-vous 吾爱

有所谓的配置值的maxRequestLength 。在配置文件中,它看起来是这样的:

There's a configuration value called maxRequestLength. In a config file it looks like this:

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

如何设置的值的maxRequestLength 编程?

推荐答案

您不能!

的maxRequestLength 是由HttpWorkerRequest之前调用实际的HttpHandler ,这意味着通用的处理程序或页面执行后,该请求点击服务器和已经由相应的处理asp.net工作者。你不能有超过任何控制在的maxRequestLength 在你的页面code或一个HttpHandler!

maxRequestLength is handled by the HttpWorkerRequest prior the call to the actual HttpHandler, meaning that a generic handler or a page is executed after the request hit the server and has processed by the corresponding asp.net worker. you cannot have any control over the maxRequestLength in your page code or an HttpHandler!

如果你想读在$ C $的要求长度C,你可以做到这一点无论是通过 HTTP模块的Global.asax 的文件,这是它是如何在Global.asax内部完成:

If you want to read the request length in code you can do that either through a HttpModule or the global.asax file, this is how it is done inside the 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)));
    }
}

您可以设置的maxRequestLength 的web.config 到最大值,并呼吁工人的CloseConnection在code方法如果请求长度达到所需的值!

You could set the maxRequestLength in your web.config to its max value and call the worker's CloseConnection method in your code if the request length reaches the desired value!