输入Web服务字段可选不需要

时间:2015-06-10 13:41:07

标签: c# json web-services visual-studio-2010

我创建了一个示例Web服务,它将向用户询问一些信息,并以JSON格式返回所需的所有信息。我这里有两个问题

  1. 我不想要求用户输入新日期和续订日期,我想这样做只需要其中一个而另一个是可选的
  2. 如果同时输入了新日期和续订日期,则用户将使用一个JSON获取所有日期,但我不确定如何将两个列表一起附加。
  3. 如果您需要其他信息,请告诉我,这是我的网络服务代码

        namespace WebServiceTest
        {
            /// <summary>
            /// Summary description for WebServicePrueba
            /// </summary>
            [WebService(Namespace = "http://tempuri.org/")]
            [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
            [System.ComponentModel.ToolboxItem(false)]
            // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
            // [System.Web.Script.Services.ScriptService]
            public class WebServicePrueba : System.Web.Services.WebService
            {
                private string User { get; set; }
                private string UserID { get; set; }
                private DateTime NewServiceDate { get; set; }
                private DateTime NewServicesTo { get; set; }
                private DateTime RenewServiceDate { get; set; }
                private DateTime RenewServiceTo { get; set; }
    
                [System.Web.Services.WebMethod(Description = "Get Services Received", EnableSession = true)]
                public string getSeviceInformation(string name, string userId, DateTime newServiceDate, DateTime newServicesTo, DateTime renewServiceDate, DateTime renewServiceTo)
                {
                    User = name;
                    UserID = userId;
                    NewServiceDate = newServiceDate;
                    NewServicesTo = newServicesTo;
                    RenewServiceDate = renewServiceDate;
                    RenewServiceTo = renewServiceTo;
    
                    if (User == "prueba" && UserID == "prueba")
                    {
                        if (renewServiceDate == DateTime.MinValue || renewServiceTo == DateTime.MinValue)
                        {
                            ArrayList list = this.participant.getAddedTRCTest(newServiceDate, newServicesTo);
                            return JsonConvert.SerializeObject(list);
                        }
    
                        else if (newServiceDate == DateTime.MinValue || newServicesTo == DateTime.MinValue)
                        {
                            ArrayList list = this.participant.getModifiedTRCTest(renewServiceDate, renewServiceTo);
                            return JsonConvert.SerializeObject(list);
                        }
    
                        else
                        {
                            ArrayList list = this.participant.getAddedTRCTest(newServiceDate, newServicesTo);
                            ArrayList list2 = this.participant.getModifiedTRCTest(renewServiceDate, renewServiceTo);
                            return JsonConvert.SerializeObject(list);
                        }
    
                    }
    
                    else if (User != "prueba" && UserID == "prueba")
                    {
                        return "Please check your username";
                    }
    
                    else if (User == "prueba" && UserID != "prueba")
                    {
                        return "Please check your UserID";
                    }
    
                    else
                    {
                        return "Please check both your username and userID";
                    }
                }
    
            }
        }
    

    以下是我运行网络服务时输入的错误,并且没有输入其中一个日期(即我输入newServiceDatenewServicesTo但不输入renewServiceDate和{{ 1}}或其他方式

    renewServiceTo

    ----------------------------- UPDATE -------------- ----------

    @Jianping Liu当我在我的函数中声明可以为null的参数时我不再能够测试webservice,我得到了 `测试

    测试表仅适用于具有基本类型作为参数的方法 这是我如何宣布它

        System.ArgumentException: Cannot convert  to System.DateTime.
        Parameter name: type ---> System.FormatException: String was not recognized as a valid DateTime.
           at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
           at System.Convert.ToDateTime(String value, IFormatProvider provider)
           at System.String.System.IConvertible.ToDateTime(IFormatProvider provider)
           at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
           at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
           --- End of inner exception stack trace ---
           at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
           at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
           at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
           at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
           at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
    

2 个答案:

答案 0 :(得分:0)

您可以为4 DateTime

使用可空变量
private DateTime? NewServiceDate { get; set; }
private DateTime? NewServicesTo { get; set; }
private DateTime? RenewServiceDate { get; set; }
private DateTime? RenewServiceTo { get; set; }

并使用NewServiceDate.HasValue检查它是否包含值。

答案 1 :(得分:0)

  1. 使用可空的DateTime
  2. getSeviceInformation方法的签名: public string getSeviceInformation(string name,string userId,DateTime?newServiceDate,DateTime?newServicesTo,DateTime?renewServiceDate,DateTime?renewServiceTo)

    1. 查看具有值的日期;
    2. if(newServiceDate.HasValue)....在这里做点什么