2017-06-16 28 views
3

如何在asp.net web應用程序中測試我的customErrors statusCode 403,400和500?如何在asp.net中測試400,403,500等錯誤?

的Web.config

<customErrors mode="On" redirectMode="ResponseRewrite"> 
    <error statusCode="404" redirect="404error.aspx" /> 
    <error statusCode="403" redirect="403error.aspx" /> 
    <error statusCode="400" redirect="400error.aspx" /> 
    <error statusCode="500" redirect="500error.aspx" /> 
</customErrors> 

我通過editng URL和創建上點擊等虛假頁面響應測試404錯誤,我不知道如何來測試其他statusCodes!所以,我如何測試customErrors?
是一個初學者,所以對於這個愚蠢的問題抱歉。

+1

在您的終端之一,拋出錯誤或異常 – Rob

+0

使用HttpResponseMessage類扔HTTP響應代碼,你有興趣的客戶。 – Thangadurai

+0

你能幫我解決這個問題嗎?!! – frank

回答

5

拋出一個新的HttpException

throw new HttpException(400, "This is a 'bad request' test"); 

等等。

0

我喜歡Robs IDea。 你可以簡單地添加一個TestController,它實現了一系列提升異常的方法。 我會在路由中添加ifdef,以便這些類型的端點僅在測試期間可用,而不是在生產中。

+0

這個問題沒有被標記爲ASP.NET MVC問題,所以這可能不完全相關。 –

0

創建一個假控制器,其中包含您要測試的每個事物的操作。

public class ErrorController : Controller 
{ 
    public ActionResult BadRequest() 
    { 
      throw new HttpException(400, "Bad request"); 
    } 

    public ActionResult Unauthorized() 
    { 
      throw new HttpException(403, "Unauthorized"); 
    } 

    public ActionResult ServerError() 
    { 
      throw new HttpException(500, "Server error"); 
    }   
} 
+0

該問題未被標記爲ASP.NET MVC問題,因此這可能不完全相關。 –

+0

有趣的看到方法名稱..! 400() – Thangadurai

+0

方法名稱用於顯示目的。修正它們 –