Faking Http Status Codes in IIS/.net for testing

眉间皱痕 提交于 2019-12-09 03:18:38

问题


This is quite an odd question, but I am trying to test the Web.Config settings for custom errors e.g.:

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

Is there anyway I can create a page or intercept the request in the global.asax Application_BeginRequest method that can fake up a response to send to the browser i.e. setup a 500.13 HTTP error status which tells IIS to use the 500.13.html page defined in the Web.Config.

Ideally, I'd like to do something like create a page that takes a query string value of the status code I want returned e.g. FakeRequest.html?errorStatus=500.13 so that our testers can make sure the appropriate page is returned for the various errors.


回答1:


Try something like:

    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");
        }
    }

Found this at the following page: http://aspnetresources.com/articles/CustomErrorPages




回答2:


This won't work for all but you can flesh it out... The cache setting is important otherwise the last code they try could be cached by the browser etc.

Create a basic page, e.g. "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>

Then hit it...

  • http://example.com/FakeError.aspx?code=302
  • http://example.com/FakeError.aspx?code=404
  • Etc.

Like I said, not all will work but see how you go.

For status codes, see http://msdn.microsoft.com/en-us/library/aa383887(VS.85).aspx



来源:https://stackoverflow.com/questions/7724998/faking-http-status-codes-in-iis-net-for-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!