【发布时间】:2013-12-07 00:20:04
【问题描述】:
有一个名为maxRequestLength 的配置值。在配置文件中它看起来像这样:
<configuration>
<system.web>
<httpRuntime maxRequestLength="2048576" />
</system.web>
</configuration>
如何以编程方式设置maxRequestLength 的值?
【问题讨论】:
有一个名为maxRequestLength 的配置值。在配置文件中它看起来像这样:
<configuration>
<system.web>
<httpRuntime maxRequestLength="2048576" />
</system.web>
</configuration>
如何以编程方式设置maxRequestLength 的值?
【问题讨论】:
你不能!
maxRequestLength 在调用实际的HttpHandler 之前由HttpWorkerRequest 处理,这意味着在请求到达服务器并且已由相应的 asp.net 工作者。您无法控制页面代码或 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)));
}
}
如果请求长度达到所需值,您可以将web.config 中的maxRequestLength 设置为最大值,并在代码中调用worker 的CloseConnection 方法!
【讨论】:
在快速谷歌之后,您似乎无法以编程方式执行此操作。 See here.
两种可能的解决方案:
<location> 元素配置特定路径。【讨论】: