如何尝试并捕获重定向到错误页面?

发布于 2024-12-23 13:16:44 字数 211 浏览 6 评论 0原文

早上好,

我有一个注册表单,可将​​用户添加到系统中。例如,当管理员在出生日期中输入字母而不是日期时,就会发生异常。我在 web.config 中执行了以下代码:

"customErrors mode="On" defaultRedirect="Error.aspx""

当错误发生时,try 和 catch 函数如何将管理员重定向到 error.aspx?

Good morning,

I have a registration form which adds the user to the system. For example when the admin enters a letter instead of a date in the date of birth, an exception occurs. I did the following code in the web.config:

"customErrors mode="On" defaultRedirect="Error.aspx""

How the try and catch function will redirect the admin to the error.aspx when the error occurs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠北森屋 2024-12-30 13:16:44

由于您已经捕获了此异常,因此它不会重定向到错误页面。如果您想这样做,请尝试重新抛出异常。

catch (Exception)
{
   // your custom code
   throw;
}

Since you have captured this exception, it will not redirect to the error page. If you want to do so, please try to rethrow the exception.

catch (Exception)
{
   // your custom code
   throw;
}
别靠近我心 2024-12-30 13:16:44

听起来您需要捕获异常,然后手动重定向(Response.Redirect)到错误页面。您可以通过会话变量或应用程序变量将异常传递到错误页面。

It sounds like you need to catch the exception then do a manual redirect (Response.Redirect) to your error page. You can pass the exception to your error page in a session variable or application variable.

倾其所爱 2024-12-30 13:16:44

除非您执行某些操作,否则如果您自己捕获异常,管理页面将不会重定向到 error.aspx。

它只会重定向未处理的异常。

更新
从您的评论来看,您似乎最好在页面上创建某种验证以及验证摘要,而不是依赖重定向到错误页面。你想要两件不能在一起的事情。

<asp:CompareValidator 
    id="dateValidator" runat="server"  
    Type="Date" 
    Operator="DataTypeCheck" 
    ControlToValidate="txtDatecompleted"  
    ErrorMessage="Please enter a valid date."> 
</asp:CompareValidator> 

Unless you do something, the admin page will NOT redirect to error.aspx if you catch the exception yourself.

it will only redirect for unhandled exceptions.

update
From your comments, it seems you are better of creating some kind of validation on your page together with a validationsummary instead of relying on a redirect to the Error page. You want two things that cannot go together.

<asp:CompareValidator 
    id="dateValidator" runat="server"  
    Type="Date" 
    Operator="DataTypeCheck" 
    ControlToValidate="txtDatecompleted"  
    ErrorMessage="Please enter a valid date."> 
</asp:CompareValidator> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文