在Web服务调用中接受空值

时间:2014-09-29 08:11:42

标签: asp.net vb.net web-services soap asmx

我的网络服务中有以下方法

<WebMethod(Description:="GetMerchants")> _
    Public Function GetMerchants(ByVal separator As String, ByVal DateFrom As String, ByVal DateTo As String, ByVal ID As String, ByVal MerchantID As String, ByVal RimNo As String, _
                                    ByVal Name As String, ByVal Description As String, ByVal Category As String,
                                    ByVal Region As String, ByVal isHidden As Boolean)

我想将空值发送给ishidden,但它给了我以下错误

  

System.ArgumentException:无法转换为System.Boolean。   参数名称:type ---&gt; System.FormatException:String未被识别为有效的布尔值。      在System.Boolean.Parse(String value)      在System.String.System.IConvertible.ToBoolean(IFormatProvider provider)      at System.Convert.ChangeType(Object value,Type conversionType,IFormatProvider provider)      at System.Web.Services.Protocols.ScalarFormatter.FromString(String value,Type type)      ---内部异常堆栈跟踪结束---      at System.Web.Services.Protocols.ScalarFormatter.FromString(String value,Type type)      在System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection集合)      在System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest请求)      在System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()      在System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

如何将ishidden的默认值设置为false。 顺便说一句,它甚至没有进入方法。当我点击调用并通过放置断点我意识到它甚至没有输入方法时显示错误。

我在网上搜索,我找不到将默认值设置为可选字段的方法。有没有办法这样做?

1 个答案:

答案 0 :(得分:0)

因为&#34; bool&#34;你不需要将它改成可以为空的类型。

在C#中我会写&#34; bool?是否隐藏&#34;而不是&#34; bool&#34;,否则(会是相同的)是&#34; Nullable isHidden&#34;。

所以将代码更改为&#34; As Boolean?&#34; (最后是一个问号)。然后它应该接受null。 =)

可空类型有两个属性,&#34; HasValue&#34;和&#34;价值&#34;。

您可以在http://msdn.microsoft.com/en-us/library/ms235245.aspx

上阅读有关可空类型的内容
相关问题