简体   繁体   English

Web方法中的可选参数

[英]Optional Parameters in Web Method

I've defined the following web method: 我定义了以下网络方法:

public string GetMatchingCompanies(string term, int companyPickerMode, int? officeId, int? proposalId)
{
    // ...
}

As you can see, the last two arguments are nullable. 如您所见,最后两个参数是可为空的。 The idea is that these arguments are optional. 想法是这些参数是可选的。 They will be null if they are not specified. 如果未指定,则它们将为null。

However, when my AJAX code calls this method without supplying one of these arguments, I get the following error. 但是,当我的AJAX代码在不提供这些参数之一的情况下调用此方法时,出现以下错误。

Invalid web service call, missing value for parameter: 'officeId'. 无效的Web服务调用,缺少参数'officeId'的值。

This is unexpected. 这是意外的。 Isn't there a way to make these parameters optional? 没有办法使这些参数可选吗?

Making then fields nullable does not mean that they do not need to be supplied, only that they will be initialized or can be set to null. 然后将字段设置为空并不意味着不需要提供它们,只是将其初始化或可以将其设置为null。 If you do not want to specify them, set a default value like so: 如果您不想指定它们,请设置默认值,如下所示:

public string GetMatchingCompanies(string term, int companyPickerMode, int? officeId = null, int? proposalId = null)
{
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM