如何在 ASP.NET 中为不同的 URL 设置不同的超时时间

How to set different Timeouts for different URLs in ASP.NET(如何在 ASP.NET 中为不同的 URL 设置不同的超时时间)
本文介绍了如何在 ASP.NET 中为不同的 URL 设置不同的超时时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序中的某些 URL 有不同的连接限制.一些 URL 接受文件上传并且需要有较大的连接超时.所有其他 URL 需要更短的超时来防止拒绝服务并且不浪费资源.

I want different connection limits for some URLs in my application. Some URLs accept file uploads and need to have a large Connection Timeout. All other URLs need a much smaller timeout to prevent denial of service and not waste resources.

目前我在 IIS 中将整个站点的连接超时属性设置为 60 分钟.然后我在 web.config 中做了这个:

Currently I have the Connection Timeout property in IIS set for the entire site to 60 minutes. Then I did this in the web.config:

<system.web>
    <httpRuntime executionTimeout="480" maxRequestLength="1024" />
</system.web>

<location path="FileUpload/WriteFile.rails">
    <system.web>
        <httpRuntime executionTimeout="3600" maxRequestLength="512000" />
    </system.web>
</location>

所以我希望这会将所有 URL 设置为 8 分钟超时,并允许 WriteFile.rails URL 运行 60 分钟.相反,所有 URL 都允许运行 60 分钟.如何让 IIS 做我想做的事?

So i was hoping this would set all URLs to an 8 minute timeout and allow the WriteFile.rails URL to run for 60 minutes. Instead ALL URLs are allowed to run for 60 minutes. How do I get IIS to do what I want?

推荐答案

这个问题专门询问了超时,但也暗示了设置 maxRequestLength.我将尝试对这两个问题给出一个非常全面的答案(现在我花了一天的大部分时间来解决它).

The question asked specifically about timeouts but also implied setting maxRequestLength as well. I'm going to try and give a really comprehensive answer to both issues (now that I have spent most of a day working it out).

假设我们的网站上有一个要处理文件上传的 URL.我们希望在该 URL 上接收多达 1 GB 的数据,并且我们将允许客户端最多连接 1 小时.我们希望所有其他 URL 仅允许 90 秒的连接时间和 POST 正文中的最大 4MB.

Lets say we have a single URL on our website that we want to process file uploads. We want to take in up to a Gigabyte of data on that URL and we will allow clients to be connected for, at most, 1 hour. All other URLs we want to only allow 90 seconds of connection time and a maximum of 4MB in the POST body.

首先,您必须在全球范围内提高整个网站的时间和规模限制.首先,您要为整个站点设置连接超时".这是一个绝对上限,不能在 web.config 中设置.IIS7 网站有这里有很好的说明.您也可以使用 Microsoft.Web.Administration 库:

First you have to globally raise the limits on time and size for the entire site. First you want to set the "Connection Timeout" for the entire site. This acts as an absolute upper bound and it cannot be set from within the web.config. The IIS7 website has good instructions here. You can also do it programatically with the Microsoft.Web.Administration library that shipped with IIS7/7.5:

var serverManager = ServerManager.OpenRemote("\web-server-name");
var site = serverManager.Sites["Your-Site-Name"];
site.Limits.ConnectionTimeout = new TimeSpan(1, 0, 0);

接下来,您需要设置站点允许的最大大小请求.这是在一个完全不同的地方,在 Request Fitlering 模块中.IIS7 上可能默认未安装此模块.Microsoft 再次为如何设置 maxAllowedContentLength 通过 GUI.这是您可以在 Web.config 中设置的内容:

Next you need to set the max size request that the site will allow. This is in a totally different place, in the Request Fitlering module. This module may not be installed by default on IIS7. Again Microsoft has good instructions for how to set the maxAllowedContentLength through the GUI. This is something you can set from within the Web.config:

<system.webServer>
    <security>
        <requestFiltering>
            <!-- Allow 1GB uploads -->
            <requestLimits maxAllowedContentLength="1073741824"></requestLimits>
        </requestFiltering>
    </security>
</system.webServer>

根据 Content-Length 标头评估此设置,大于此值的请求将立即导致 404.13.设置以字节为单位,接下来以千字节为单位,与 IIS7 非常一致.

This setting is evaluated against the Content-Length header and requests larger than this will immediately result in a 404.13. The setting is in bytes and what comes next is in Kilobytes, very consistent this IIS7.

接下来,我们要将所有 ASP.NET 请求限制为 90 秒/4MB.这可以在 web.config 中完成:

Next we want to cap all of the ASP.NET requests at 90 seconds/4MB. This can be done in the web.config:

<location>
    <system.web>
        <httpRuntime executionTimeout="90" maxRequestLength="4096" />
    </system.web>
</location>

为了使设置全局化,system.web 标签被包裹在一个没有 path 属性的 location 标签中.(在最初的问题中,我没有将 system.web 标记包装在 location 标记中,这可能是我的问题的根源.) maxRequestLength 这次以千字节为单位.

To make the settings global the system.web tag is wrapped in a location tag that has no path attribute. (In the original question I did not wrap the system.web tag in the location tag which was probably the source of my problem.) maxRequestLength is in kilobytes this time.

最后,我们希望允许我们的特殊上传 URL 接受大量上传.将这些值设置为高于您全局设置的值将不起作用.全局值会覆盖这些设置.

Finally we want to allow our special upload URL to accept huge uploads. Setting these values higher than the ones you set globally wont work. The global values override these settings.

<location path="Uploads/PostFile.rails">
    <system.web>
        <httpRuntime executionTimeout="3600" maxRequestLength="1048576" />
    </system.web>
</location>

如果其他一切都设置正确,那就应该这样做.正如 Peter Bromberg 建议的那样,您可以根据需要添加任意数量的这些块,以提高特定 URL 的限制.

If everything else is set up right, that should do it. As Peter Bromberg suggested, you can add as many of these blocks as needed to raise the limits for specific URLs.

最后一点:在调试模式下,IIS 不会强制执行连接超时或执行超时设置,以允许您有更多时间进行调试.因此,要在开发人员机器上测试您的设置,您应该进行发布构建,并且您应该将启用服务器端调试"设置设置为 false.

One last note: in debug mode IIS does not enforce the Connection Timeout or executionTimeout settings, to allow you more time for debugging. So to test your setting on a developer machine you should do a release build and you should set the 'Enable Server-Side Debugging' setting to false.

这篇关于如何在 ASP.NET 中为不同的 URL 设置不同的超时时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)