117

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?

1

3 Answers 3

155

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

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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 HttpContext is instantiated on a per request basis, so it would be back to the default value on the next request
You might wanna add HttpContext.Current ('Current' is missing)
I get: "httpcontext does not contain a definition for current". I'm using .NET Core 2.0. Any idea how to fix this?
|
81
<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.

6 Comments

-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.
executionTimeout does not work for MVC - this is the wrong answer. see here: forums.asp.net/p/1715081/…
please note this is ignored completed if debug mode is on msdn.microsoft.com/en-us/library/vstudio/… 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.
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.
@EricJ. only within the Controller Route(s) that start with ControllerName\ActionName
|
24

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

HttpContext.Current.Server.ScriptTimeout = 300;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.