如何在ASP.NET Web App中生成未处理的异常?

时间:2022-01-26 03:36:30

I put some global error handling in place, but am having problems testing it by causing unhandled exceptions.

我将一些全局错误处理到位,但是在通过导致未处理的异常来测试它时遇到了问题。

Here's all I can think of at the moment. Please feel free to add to this list with more ways to trip unhandled exceptions.

这就是我现在所能想到的一切。请随意添加到此列表中,并提供更多方法来处理未处理的异常。

  1. Dangerous form data - Entering characters such as < and > in a text box and trying to submit
  2. 危险表单数据 - 在文本框中输入 <和> 等字符并尝试提交
  3. Put invalid values in a URL parameter - eg if you page is www.test.com/home?testid=XXX where XXX is the form number/identity # you are trying to pull up, put a number that doesn't exist in the URL and hit enter.
  4. 在URL参数中放置无效值 - 例如,如果您的页面是www.test.com/home?testid=XXX,其中XXX是您尝试提取的表单编号/标识#,请输入一个不存在的数字URL并点击回车。

I'm sure I could change some stored procedures or otherwise mess with my data access components but I'd rather not have to change any code anywhere...I want to be able to generate these exceptions on the front end, like a user would.

我确信我可以更改一些存储过程或以其他方式弄乱我的数据访问组件,但我不想在任何地方更改任何代码...我希望能够在前端生成这些异常,就像用户一样将。

Let me know what ideas you have, or if you all need any other info.

让我知道您有什么想法,或者您是否还需要任何其他信息。

3 个解决方案

#1


2  

One possible way to do this would be to use querystring parameters that you append to your URL to force an exception to be thrown, probably restricted to debug builds or via a setting in web.config so that end users who learn about your parameters can't play around.

一种可能的方法是使用附加到URL的查询字符串参数来强制抛出异常,可能仅限于调试版本或通过web.config中的设置,以便了解您的参数的最终用户可以'玩耍。

Something like

就像是

http://mypage.aspx?...&ThrowException=true&ComponentName=Whatever...

You'll have to design the syntax, then manually append the parameters to URLs in the address bar. Then you can add calls to a static helper method something like the following at strategic places in your code where you want to test exception handling (e.g. the data access layer)

您必须设计语法,然后手动将参数附加到地址栏中的URL。然后,您可以在代码中要在其中测试异常处理的策略位置(例如数据访问层)添加对静态帮助程序方法的调用,如下所示:

[Conditional("Debug")]
static void SimulateError(string componentName)
{
    if(Request.QueryString["ComponentName"] == componentName)
    {
        throw ...;
    }
}

#2


2  

Use

使用

throw new Exception("this is my exception");

somewhere in the code that gets executed upon calling the requested page

在调用所请求页面时执行的代码中的某个位置

#3


1  

You aren't going to be able to test all your exceptions from the end user environemnt because that's not where most of your unhandled exceptions should be coming from.

您无法从最终用户environemnt测试所有异常,因为这不是大多数未处理的异常应该来自的地方。

This question might be useful to you as well.

这个问题也可能对你有用。

#1


2  

One possible way to do this would be to use querystring parameters that you append to your URL to force an exception to be thrown, probably restricted to debug builds or via a setting in web.config so that end users who learn about your parameters can't play around.

一种可能的方法是使用附加到URL的查询字符串参数来强制抛出异常,可能仅限于调试版本或通过web.config中的设置,以便了解您的参数的最终用户可以'玩耍。

Something like

就像是

http://mypage.aspx?...&ThrowException=true&ComponentName=Whatever...

You'll have to design the syntax, then manually append the parameters to URLs in the address bar. Then you can add calls to a static helper method something like the following at strategic places in your code where you want to test exception handling (e.g. the data access layer)

您必须设计语法,然后手动将参数附加到地址栏中的URL。然后,您可以在代码中要在其中测试异常处理的策略位置(例如数据访问层)添加对静态帮助程序方法的调用,如下所示:

[Conditional("Debug")]
static void SimulateError(string componentName)
{
    if(Request.QueryString["ComponentName"] == componentName)
    {
        throw ...;
    }
}

#2


2  

Use

使用

throw new Exception("this is my exception");

somewhere in the code that gets executed upon calling the requested page

在调用所请求页面时执行的代码中的某个位置

#3


1  

You aren't going to be able to test all your exceptions from the end user environemnt because that's not where most of your unhandled exceptions should be coming from.

您无法从最终用户environemnt测试所有异常,因为这不是大多数未处理的异常应该来自的地方。

This question might be useful to you as well.

这个问题也可能对你有用。