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

时间:2021-12-20 04:13:48

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 example:

网络。配置的例子:

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

How do I do it?

我该怎么做呢?

3 个解决方案

#1


104  

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秒(奇怪的默认值是什么?)

#2


52  

<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中设置这样的值更好。配置的控制器。可配置选项的硬编码被认为是有害的。

#3


16  

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

我必须使用。net 4.5添加“当前”:

HttpContext.Current.Server.ScriptTimeout = 300;

#1


104  

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秒(奇怪的默认值是什么?)

#2


52  

<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中设置这样的值更好。配置的控制器。可配置选项的硬编码被认为是有害的。

#3


16  

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

我必须使用。net 4.5添加“当前”:

HttpContext.Current.Server.ScriptTimeout = 300;