捕获请求解析过程中的异常

发布于 2024-10-12 00:31:50 字数 864 浏览 6 评论 0原文

我有一个 ASP.NET Web 服务,请求中的一些字段被定义为枚举。当输入空白或无效值时,响应返回为:

Parameter name: type ---> System.ArgumentException: Must specify valid information for parsing in the string.
   at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

是否可以捕获这样的错误并返回基于 XML 的响应?

I have an ASP.NET webservice and some of the fields in the request are defined as enums. When entering a blank or invalid value, the response comes back as:

Parameter name: type ---> System.ArgumentException: Must specify valid information for parsing in the string.
   at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

Is it possible to capture errors like this and return an XML based response instead?

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

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

发布评论

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

评论(2

蓝梦月影 2024-10-19 00:31:50

不,ASMX Web 服务无法做到这一点。

当然,您可以使用 WCF 来完成此操作。

当然,如果你的客户端发送了有效的数据那就更好了。您可能想找出为什么他们不这样做。

No, there's no way to do this with ASMX web services.

Naturally, you can do this with WCF.

Of course, it would be better if your client sent valid data. You might want to find out why they aren't.

累赘 2024-10-19 00:31:50

当然,它看起来像这样:

try
{
    Enum.Parse(Type enumType, String value, Boolean ignoreCase)
}
catch (ArgumentException e)
{
   //Serialise exception information from 'e' into XML
   //(not shown here) and set it as the response
   Response.Write(xmlMessage);
   Response.End();
}

Sure, it would look something like this:

try
{
    Enum.Parse(Type enumType, String value, Boolean ignoreCase)
}
catch (ArgumentException e)
{
   //Serialise exception information from 'e' into XML
   //(not shown here) and set it as the response
   Response.Write(xmlMessage);
   Response.End();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文