有一个配置值叫做maxRequestLength
。在配置文件中看起来像这样:
<configuration>
<system.web>
<httpRuntime maxRequestLength="2048576" />
</system.web>
</configuration>
我该如何以编程方式设置
maxRequestLength
的值?maxRequestLength
!
在调用实际的 HttpHandler
之前,maxRequestLength
由HttpWorkerRequest处理,这意味着请求被服务器接收并且已由相应的asp.net worker进行处理后,才会执行通用处理程序或页面。你无法在页面代码或者一个HttpHandler中对maxRequestLength
进行任何控制!HttpModule
或 global.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)));
}
}
如果请求长度达到所需值,您可以在代码中调用工作线程的CloseConnection方法,并将web.config
中的maxRequestLength
设置为最大值!