模拟ASP.NET MVC上的超时

时间:2011-07-19 21:57:59

标签: asp.net-mvc-2

问题:

我需要创建一个带有超时控制器的Web项目。

我做了什么:

  • 创建新的Web应用程序
  • 清空web.config,并写下以下值:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <httpRuntime executionTimeout="1" />
        <compilation debug="false" />
        <httpModules>
            <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </httpModules>
    </system.web>
</configuration>
  • 将以下代码写入控制器:

public class DefaultController : Controller
{
    public EmptyResult Index()
    {
        System.Threading.Thread.Sleep(3000);
        Response.Write("ScriptTimeout: " + HttpContext.Server.ScriptTimeout);
        return new EmptyResult();
    }
}

运行时,服务器休眠3秒钟,然后返回响应,没有任何超时错误。 ScriptTimeout值为1.

问题:

知道出了什么问题吗?

2 个答案:

答案 0 :(得分:1)

您想要一个408 Request Timeout HTTP状态代码吗?

public ActionResult Index()
{
    return new HttpStatusCodeResult(408);
}

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

答案 1 :(得分:1)

只有executionTimeout元素<httpRuntime/>上的debug="false"才会考虑<compilation/>元素的{{1}}属性。

尝试删除debug属性或将其明确设置为false。当然,您必须在没有调试的情况下运行应用程序来测试它。

有关详细信息,请参阅http://msdn.microsoft.com/en-gb/library/vstudio/e1f13641(v=vs.100).aspx