Faking Http Status Codes in IIS/.net for testing
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.
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
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
精彩评论