伪造IIS / .net中的Http状态代码进行测试

时间:2011-10-11 11:03:45

标签: asp.net iis http-status-codes

这是一个非常奇怪的问题,但我正在尝试测试自定义错误的Web.Config设置,例如:

  <customErrors mode="On"/>
    <error statusCode="500" redirect="500.html"/>  
    <error statusCode="500.13" redirect="500.13.html"/>        
  </customErrors>

无论如何,我可以在global.asax Application_BeginRequest方法中创建页面或拦截请求,该方法可以伪造响应以发送到浏览器,即设置500.13 HTTP错误状态,告知IIS使用Web.Config中定义的500.13.html页面。

理想情况下,我想做一些事情,例如创建一个页面,该页面采用我想要返回的状态代码的查询字符串值,例如FakeRequest.html?errorStatus=500.13以便我们的测试人员可以确保针对各种错误返回相应的页面。

2 个答案:

答案 0 :(得分:3)

尝试类似:

    protected void Page_Load(object sender, EventArgs e)
    {
        var rawErorStatus = HttpContext.Current.Request.QueryString.Get("errorStatus");

        int errorStatus;
        if (int.TryParse(rawErorStatus, out errorStatus))
        {
            throw new HttpException(errorStatus, "Error");
        }
    }

在以下页面找到:http://aspnetresources.com/articles/CustomErrorPages

答案 1 :(得分:0)

这对所有人都不起作用,但你可以充实它...缓存设置很重要,否则他们尝试的最后一个代码可以被浏览器等缓存。

创建一个基本页面,例如“FakeError.aspx”:

<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server" language="c#">

  protected void Page_Load(object sender, EventArgs e)
  {
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.StatusCode = Convert.ToInt32(Request.QueryString["code"]);
    Response.End();
  }

</script>
</html>

然后点击它......

就像我说的那样,并不是所有人都会工作,但看你怎么走。

有关状态代码,请参阅http://msdn.microsoft.com/en-us/library/aa383887(VS.85).aspx