简体   繁体   中英

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.

However, when my AJAX code calls this method without supplying one of these arguments, I get the following error.

Invalid web service call, missing value for parameter: '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. 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)
{
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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