简体   繁体   中英

Make input to Web Service field optional not required

I created a sample webservice that will ask the user for some information and return in JSON form all the information required. I have two issues here

  1. I don't want to make it required that the user enter bot the new dates and renewal dates, I want to make so only one of them is required while the other one is optional
  2. If both dates, new and renewal, are entered then the user will get it all in one JSON but I am not sure how to append both lists together.

Please let me know if you need additional information, here is my code for the webservice

    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";
                }
            }

        }
    }

Here is the error I get when I run the webservice and don't enter one of the dates (ie I enter newServiceDate and newServicesTo but not renewServiceDate and renewServiceTo or the other way around

    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()

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

@Jianping Liu when I declare the nullable parameters in my function I am no longer able to test the webservice, I get `Test

The test form is only available for methods with primitive types as parameters.` Here is how I am declaring it

 [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)

You can use nullable variable for your 4 DateTime

private DateTime? NewServiceDate { get; set; }
private DateTime? NewServicesTo { get; set; }
private DateTime? RenewServiceDate { get; set; }
private DateTime? RenewServiceTo { get; set; }

and use NewServiceDate.HasValue to check if it contains a value.

  1. Use nullable DateTime

Signature of getSeviceInformation method: public string getSeviceInformation(string name, string userId, DateTime? newServiceDate, DateTime? newServicesTo, DateTime? renewServiceDate, DateTime? renewServiceTo)

  1. Check what dates has values;

if(newServiceDate.HasValue) .... do something here

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