There's a configuration value called maxRequestLength
. In a config file it looks like this:
<configuration>
<system.web>
<httpRuntime maxRequestLength="2048576" />
</system.web>
</configuration>
How can I set the value of maxRequestLength
programmatically?
You can't!
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!
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)));
}
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With