Faking Http Status Codes in IIS/.net for testing
这是一个很奇怪的问题,但是我正在尝试测试Web.Config设置中的自定义错误,例如:
1 2 3 4 | <customErrors mode="On"/> <error statusCode="500" redirect="500.html"/> <error statusCode="500.13" redirect="500.13.html"/> </customErrors> |
无论如何,我可以在global.asax
理想情况下,我想做一些类似的事情,例如创建一个页面,该页面接受要返回的状态代码的查询字符串值,例如
尝试类似的方法:
1 2 3 4 5 6 7 8 9 10 | 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
这并不适用于所有人,但您可以充实它...缓存设置很重要,否则他们尝试的最后一个代码可能会被浏览器等缓存。
创建一个基本页面,例如" FakeError.aspx":
1 2 3 4 5 6 7 8 9 10 11 12 | <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(); } </html> |
然后点击...
- http://example.com/FakeError.aspx?code=302
- http://example.com/FakeError.aspx?code=404
- 等等。
就像我说的那样,并非所有方法都能奏效,但请看您的工作方式。
有关状态代码,请参见http://msdn.microsoft.com/zh-cn/library/aa383887(VS.85).aspx