ASP.NET - 如何显示错误页面在上传大文件时(最大请求长度超过了)?长度、大文件、错误、上传

2023-09-04 22:31:51 作者:真实的假面

应用程序能够在的OnError记录错误,但我们无法做任何重定向左右,显示一些meaningfull用户。 有任何想法吗? 我知道,我们可以在web.config中设置maxRequestLength的,但无论如何,用户可以超过这个限制,一些正常的错误需要显示。

Application able to record error in OnError, but we are not able to do any redirect or so to show something meaningfull to user. Any ideas? I know that we can set maxRequestLength in web.config, but anyway user can exceed this limit and some normal error need to be displayed.

推荐答案

正如你所说的,你可以在你的web.config中设置了maxRequestLength(重写你的machine.config中的默认4MB),如果这种限制被超过,你通常可以得到HTTP 401.1错误。

As you say you can setup maxRequestLength in your web.config (overriding the default 4MB of your machine.config) and if this limits is exceeded you usually get a HTTP 401.1 error.

为了处理在应用程序级的通用的HTTP错误,您可以设置system.web节内你的web.config一个CustomError部分:

In order to handle a generic HTTP error at application level you can setup a CustomError section in your web.config within the system.web section:

<system.web>
   <customErrors mode=On defaultRedirect=yourCustomErrorPage.aspx />
</system.web>

每次错误是显示用户将被重定向到您的自定义错误页。

Everytime the error is showed the user will be redirected to your custom error page.

如果你想为每个错误,你可以这样做一个专门的页面:

If you want a specialized page for each error you can do something like:

<system.web>    
   <customErrors mode="On" defaultRedirect="yourCustomErrorPage.aspx">
     <error statusCode="404" redirect="PageNotFound.aspx" />
   </customErrors>
</system.web>

等等。

另外,您可以编辑从IIS虚拟目录属性的customErrors标签以指向您选择的错误处理页面。

Alternatively you could edit the CustomErrors tab of your virtual directory properties from IIS to point to your error handling pages of choice.

以上似乎不工作401.x错误 - 这code-项目文章解释了一种变通方法的什么似乎是一个非常类似的问题:的重定向到自定义401页

The above doesn't seem to work for 401.x errors - this code-project article explains a workaround for a what seems to be a very similar problem: Redirecting to custom 401 page