109

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?

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Kyle West
  • 8,934
  • 13
  • 65
  • 97
  • possible duplicate of [ASP.NET MVC and httpRuntime executionTimeout](http://stackoverflow.com/questions/492346/asp-net-mvc-and-httpruntime-executiontimeout) – balexandre Nov 15 '11 at 22:18

3 Answers3

149

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?)

Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • 35
    With the advent of the AsyncController it's worth remembering that to get a similar effect for asynchronous requests you should use the [AsyncTimeout] property. – Jason May 28 '10 at 12:31
  • 6
    My question with this answer is how would it truly only affect the one action in which it was placed in? So after the request is done does that setting get put back for all future requests? – jhilden Dec 18 '13 at 23:07
  • 12
    @jhilden HttpContext is instantiated on a per request basis, so it would be back to the default value on the next request – tobiak777 Jul 16 '15 at 10:19
  • 7
    You might wanna add HttpContext.Current ('Current' is missing) – bboyle1234 Sep 15 '16 at 23:56
  • 6
    I get: "httpcontext does not contain a definition for current". I'm using .NET Core 2.0. Any idea how to fix this? – tedi Nov 13 '17 at 08:44
  • @JedatKinports: try changing the case of your current to upper case "Current". I know this is quite old. – Simua May 21 '18 at 17:26
  • 1
    @Tadej, I inject `IHttpContextAccessor` and use that, but `Server` does not seem to be a property on it anymore. – adam0101 Oct 29 '19 at 14:10
  • how if aspnet core api sir? – toha May 13 '22 at 14:53
78
<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.

Kevin
  • 5,874
  • 3
  • 28
  • 35
Wojtek Trelak
  • 1,219
  • 9
  • 5
  • 24
    -1 Hard coding is okay for special circumstances as the OP described. It sounds like a specific action needs a different timeout than the rest of the actions so hard coding inside the action sounds like a good place. – Levitikon Sep 26 '12 at 16:56
  • 6
    executionTimeout does not work for MVC - this is the wrong answer. see here: http://forums.asp.net/p/1715081/4723882.aspx?Re+web+config+executionTimeout+not+working+in+ASP+NET+MVC – jfren484 Dec 18 '13 at 23:05
  • 4
    please note this is ignored completed if debug mode is on http://msdn.microsoft.com/en-us/library/vstudio/e1f13641(v=vs.100).aspx executionTimeout Optional Int32 attribute. Specifies the maximum number of seconds that a request is allowed to execute before being automatically shut down by ASP.NET. This time-out applies only if the debug attribute in the compilation element is False. Therefore, if the debug attribute is True, you do not have to set this attribute to a large value in order to avoid application shutdown while you are debugging. – Nick van Esch Sep 26 '14 at 06:46
  • 5
    This is not the most correct answer, because it has the side effect of changing the execution timeout for all other controller actions as well. – Eric J. Jan 05 '15 at 01:29
  • 1
    @EricJ. only within the Controller Route(s) that start with `ControllerName\ActionName` – GoldBishop Nov 14 '17 at 15:27
  • I would argue that this answer is the Hard-Coding. You can't change a Web.Config dynamically in the middle of running a system. Whereas ActionFilters or similar Attributes can access dynamic information, including configuration databases, to make the timeout dynamic. – Suamere Mar 03 '19 at 19:02
23

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

HttpContext.Current.Server.ScriptTimeout = 300;
Patrick Michalina
  • 1,279
  • 2
  • 12
  • 15