Je limite la taille de fichier que les utilisateurs peuvent télécharger sur le site à partir de Web.config. Comme expliqué here, il devrait lancer une exception ConfigurationErrorsException si la taille n'est pas acceptée. J'ai essayé de l'attraper de la méthode d'action ou du contrôleur pour des demandes de téléchargement mais aucune chance. La connexion est réinitialisée et je n'arrive pas à l'afficher pour afficher une page d'erreur.Comment faire pour attraper ConfigurationErrorsException pour violer maxRequestLength?
J'ai essayé de l'attraper dans l'événement BeginRequest mais peu importe ce que je fais, l'exception n'est pas gérée. Voici le code:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
try
{
if (context.Request.ContentLength > maxRequestLength)
{
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
// Check if body contains data
if (workerRequest.HasEntityBody())
{
// get the total body length
int requestLength = workerRequest.GetTotalEntityBodyLength();
// Get the initial bytes loaded
int initialBytes = 0;
if (workerRequest.GetPreloadedEntityBody() != null)
initialBytes = workerRequest.GetPreloadedEntityBody().Length;
if (!workerRequest.IsEntireEntityBodyIsPreloaded())
{
byte[] buffer = new byte[512];
// Set the received bytes to initial bytes before start reading
int receivedBytes = initialBytes;
while (requestLength - receivedBytes >= initialBytes)
{
// Read another set of bytes
initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
// Update the received bytes
receivedBytes += initialBytes;
}
initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
}
}
}
}
catch(HttpException)
{
context.Response.Redirect(this.Request.Url.LocalPath + "?action=exception");
}
}
Mais je reçois encore ceci:
Maximum request length exceeded.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Maximum request length exceeded.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Mise à jour:
Quelle méthode soulève l'exception de toute façon? Si je lis la requête, elle soulève une exception Si je ne la lis pas du tout, je reçois "101 Connection Reset" dans le navigateur. Que peut-on faire ici?
Je voulais suivre votre réponse, je googlé et trouvé ceci: http://stackoverflow.com/questions/2966076/getting-file-size-en-javascript. Selon ce sujet, il n'y a pas de moyen de trouver la taille du fichier par JavaScript (je ne veux pas utiliser Flash, ActiveX ou HTML 5!). Est-ce que je comprends bien? – celticharp
@necronio Je le pense. –