如何使ASP.NETWebService函数参数非强制性
我在Web服务中有这个功能如何使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) {
[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,你应该考虑不同的设计: