asp.net-mvc 如何在 asp.net mvc 应用程序中为一个控制器操作设置请求超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/579523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:21:10  来源:igfitidea点击:

How do I set the request timeout for one controller action in an asp.net mvc application

asp.net-mvcasp.net-web-api

提问by Kyle West

I want to increase the request timeout for a specific controller action in my application. I know I can do it in the web.config for the entire application, but I'd rather change it on just this one action.

我想增加应用程序中特定控制器操作的请求超时。我知道我可以在整个应用程序的 web.config 中完成它,但我宁愿仅在这一个操作上更改它。

Web.config example:

Web.config 示例:

<system.web>
  <httpRuntime executionTimeout="1000" /> 
</system.web>

How do I do it?

我该怎么做?

回答by AnthonyWJones

You can set this programmatically in the controller:-

您可以在控制器中以编程方式设置:-

HttpContext.Current.Server.ScriptTimeout = 300;

Sets the timeout to 5 minutes instead of the default 110 seconds (what an odd default?)

将超时设置为 5 分钟而不是默认的 110 秒(多么奇怪的默认值?)

回答by Wojtek Trelak

<location path="ControllerName/ActionName">
    <system.web>
        <httpRuntime executionTimeout="1000"/>
    </system.web>
</location>

Probably it is better to set such values in web.config instead of controller. Hardcoding of configurable options is considered harmful.

可能最好在 web.config 而不是控制器中设置这些值。可配置选项的硬编码被认为是有害的。

回答by Patrick Michalina

I had to add "Current" using .NET 4.5:

我必须使用 .NET 4.5 添加“当前”:

HttpContext.Current.Server.ScriptTimeout = 300;