简体   繁体   English

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

[英]How to set different Timeouts for different URLs in ASP.NET

I want different connection limits for some URLs in my application. 我希望我的应用程序中的某些URL具有不同的连接限制。 Some URLs accept file uploads and need to have a large Connection Timeout. 某些URL接受文件上载,并且需要具有较大的连接超时。 All other URLs need a much smaller timeout to prevent denial of service and not waste resources. 所有其他URL都需要更小的超时,以防止拒绝服务而不浪费资源。

Currently I have the Connection Timeout property in IIS set for the entire site to 60 minutes. 目前我在IIS中为整个站点设置了连接超时属性为60分钟。 Then I did this in the web.config: 然后我在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>

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. 所以我希望这会将所有URL设置为8分钟超时,并允许WriteFile.rails URL运行60分钟。 Instead ALL URLs are allowed to run for 60 minutes. 相反,允许所有URL运行60分钟。 How do I get IIS to do what I want? 如何让IIS执行我想要的操作?

The question asked specifically about timeouts but also implied setting maxRequestLength as well. 该问题具体涉及超时,但也暗示设置maxRequestLength。 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). 我将尝试对这两个问题给出一个非常全面的答案(现在我已经花了大部分时间来解决它)。

Lets say we have a single URL on our website that we want to process file uploads. 假设我们的网站上有一个我们想要处理文件上传的URL。 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. 我们希望在该URL上获取最多1千兆字节的数据,我们将允许客户连接最多1小时。 All other URLs we want to only allow 90 seconds of connection time and a maximum of 4MB in the POST body. 我们想要的所有其他URL只允许90秒的连接时间,并且POST主体中最多只有4MB。

Global Settings 全局设置

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. 这充当绝对上限,无法在web.config中设置。 The IIS7 website has good instructions here . IIS7网站在这里很好的说明 You can also do it programatically with the Microsoft.Web.Administration library that shipped with IIS7/7.5: 您也可以使用IIS7 / 7.5附带的Microsoft.Web.Administration 以编程方式执行此操作:

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

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. 这与Request Fitlering模块完全不同。 This module may not be installed by default on IIS7. IIS7上默认情况下可能未安装此模块。 Again Microsoft has good instructions for how to set the maxAllowedContentLength through the GUI. Microsoft再次提供了有关如何通过GUI设置maxAllowedContentLength 良好说明 This is something you can set from within the Web.config: 这是您可以在Web.config中设置的内容:

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

This setting is evaluated against the Content-Length header and requests larger than this will immediately result in a 404.13. 此设置将根据Content-Length标头进行评估,大于此值的请求将立即生成404.13。 The setting is in bytes and what comes next is in Kilobytes, very consistent this IIS7. 设置以字节为单位,接下来是Kilobytes,这与IIS7非常一致。

ASP.NET Settings ASP.NET设置

Next we want to cap all of the ASP.NET requests at 90 seconds/4MB. 接下来,我们希望以90秒/ 4MB的速度限制所有ASP.NET请求。 This can be done in the web.config: 这可以在web.config中完成:

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

To make the settings global the system.web tag is wrapped in a location tag that has no path attribute. 要使设置成为全局, system.web标记将包装在没有path属性的location标记中。 (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. (在最初的问题中,我没有在位置标记中包装system.web标记,这可能是我问题的根源。) maxRequestLength这次以千字节为单位。

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>

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. 正如Peter Bromberg所建议的那样,您可以根据需要添加尽可能多的块来提高特定URL的限制。

One last note: in debug mode IIS does not enforce the Connection Timeout or executionTimeout settings, to allow you more time for debugging. 最后一点:在调试模式下,IIS不会强制执行Connection Timeout或executionTimeout设置,以便您有更多时间进行调试。 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 . 因此,要在开发人员计算机上测试您的设置,您应该执行发布版本,并且应将“启用服务器端调试”设置为false

To set the timeout for a specific page, you can use as many <location path web.config elements as necessary: 要设置特定页面的超时,您可以根据需要使用尽可能多的<location path web.config元素:

<location path="Myfile.aspx">
  <system.web>
    <httpRuntime executionTimeout="180"/>
  </system.web>
</location>

As long as your path is defined correctly, and the .rails extension is mapped to ASP.NET, this should work. 只要您的路径定义正确,并且.rails扩展名映射到ASP.NET,这应该可行。 Could it be that the ASP.NET execution engine is not processing *.rails extensions? 可能是ASP.NET执行引擎没有处理* .rails扩展吗?

NOTE: Man what a lousy post editor. 注意:男人是一个糟糕的帖子编辑器。 Why can't you do the escaping for the user? 为什么不能为用户进行转义?

You could always set the ScriptTimeout directly in your controller action. 您始终可以在控制器操作中直接设置ScriptTimeout。 That way the configuration won't break if you change your routing. 这样,如果更改路由,配置将不会中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止ASP.NET中的浏览器超时? - How to prevent browser timeouts in ASP.NET? 在ASP.Net中为不同的会话变量设置不同的超时 - Set different timeout for different session variables in ASP.Net 如何在 Visual Studio Asp.net 项目中为不同的团队成员设置不同的 web 服务器配置? - How to set up different web server config for different team members in Visual Studio Asp.net Project? 如何将ASP.NET项目拆分为具有不同模块的不同版本 - How to split an ASP.NET project into different editions with different modules 如何在日历asp.net中用不同的颜色突出显示不同的日子 - How to highlight different days with different colors in calendar asp.net 如何在ASP.NET MVC 6中为不同的环境注册不同的服务? - How to register different services for different environments in ASP.NET MVC 6? Asp.Net设置不同的CSS类以在数据列表中生成项目 - Asp.Net set different css classes for generating items in Datalist 2013 年这里如何控制 ASP.NET 请求超时? - How to control ASP.NET request timeouts here in 2013? 如何使用ASP.NET MVC为View中的DropDownList设置不同的预配置默认值? - How to set different preconfigured default values for DropDownList in View using ASP.NET MVC? 如何在ASP.NET中为从同一页面访问的不同数据库设置访问者总数? - How to Set the Total Visitors count in ASP.NET for different Databases that are accessed from same Page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM