Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使ASP.NETWebService函数参数非强制性_Asp.net_Web Services - Fatal编程技术网

如何使ASP.NETWebService函数参数非强制性

如何使ASP.NETWebService函数参数非强制性,asp.net,web-services,Asp.net,Web Services,我在Web服务中有这个功能 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public void getbalance(string login, string password) { try { //some code } catch (Exception ex) {

我在Web服务中有这个功能

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void getbalance(string login, string password)
    {
        try
        {
            //some code
        }
        catch (Exception ex)
        {
            //show error message
        }
    }

现在,我想在这里添加一个新参数,即date。但是由于这个
webservice
被用于android应用程序,添加一个新参数将使这个功能变得多余。我知道一个明显的解决方案是创建另一个函数,但是否有其他方法可以添加新参数,并且如果调用此函数的查询字符串没有日期参数,而不是给出一个错误,那么它就可以工作。

可为空的可选参数是一种方法:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void getbalance(string login, string password, DateTime? date = null)
{
    try
    {
        //some code

        if(date.HasValue)
        {
            // Do something with date. You can get the date using date.Value
        }
    }

    catch (Exception ex)
    {
        //show error message
    }
}
如果传入日期,则日期将具有值。否则,日期将为空

尽管IMO,你应该考虑不同的设计: