有一个名为maxRequestLength的配置值。在配置文件中,它看起来像这样:
<configuration>
<system.web>
<httpRuntime maxRequestLength="2048576" />
</system.web>
</configuration>如何以编程方式设置maxRequestLength的值?
发布于 2012-12-26 02:33:53
你不能!
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设置为其最大值,如果请求长度达到所需的值,则在代码中调用工作者的CloseConnection方法!
发布于 2012-12-26 02:23:43
在快速谷歌之后,你似乎不能通过编程来做这件事。See here。
两种可能的解决方案:
<location>元素配置特定路径。https://stackoverflow.com/questions/14032918
复制相似问题