当远程 XML 源不可用时处理 XmlDataSource
使用 XmlDataSource 时,是否有好的方法来处理远程 XML 文件不可用时引起的异常?我对 .NET 和使用 C# 有点陌生。
When using an XmlDataSource is there good way to handle exceptions that are caused when the remote XML file is unavailable? I'm somewhat new to .NET and using C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当出现这样的异常时,真正由您决定什么适合您的应用程序。你唯一不应该做的就是忽略它。
您可以选择的选项包括:
您可能还需要做的一件事是清理Finally 块中的所有资源(例如打开的连接)。
It's really up to you to determine what is suitable for your application when an exception like this is raised. The only thing you shouldn't do is ignore it.
Options you have include:
One thing you may also need to do is clean up any resources (e.g. open connections) in a Finally block.
我尝试了捕获异常来触发不同处理的方法,但由于某种原因它不起作用。我仍然得到一个杀死页面的异常,而不是导致 ErrorMessage 显示和 Repeater1 隐藏的异常:
异常详细信息:System.Net.WebException:远程服务器返回错误:(404) 未找到。
为什么我不能捕获异常然后采取不同的操作?
I tried the approach of catching the exception to trigger different handling, but it doesn't work for some reason. Instead of the exception causing the ErrorMessage to show and the Repeater1 to be hidden, I still just get an exception that kills the page:
Exception Details: System.Net.WebException: The remote server returned an error: (404) Not Found.
Why can't I catch the exception and then take a different action?
我假设这个问题已经解决,但我会回答这个问题,以防其他人遇到这个问题并偶然发现这篇文章。
您还需要在 try 块中绑定到 XmlDataSource...
...确保从转发器中删除 DataSourceID 属性,然后就可以开始了。
I'm assuming this has been resolved, but am answering it in case someone else has the issue and stumbles upon this post.
You need to also bind to your XmlDataSource within your try block...
...make sure to remove the DataSourceID property from your repeater, and you should be good to go.