none
WCF Rest URL with alternate params along with optional parameters RRS feed

  • Question

  • How to use combination of optional and alternate parameters in WCF Rest URL?

    GetMyDetails(String sInput) - sInput could be either 
    <MyURL>/MyDetails/?Name=Test  - This should result list of data 
    <MyURL>/MyDetails/?Id=123 - This should also result same list of data 
    My API looks like
    [WebGet(UriTemplate = "/Detailss", ResponseFormat = WebMessageFormat.Xml)]
    int[] GetDetails();

    Use of optional parameters along with alternate parameters.

    <MyURL>/MyDetails?Name=Test  - This should result all the data

    <MyURL>/MyDetails?Name=Test & Mode="Home" - This should result only home details 

    <MyURL>/MyDetails?Name=Test & Mode="Office" - This should result only Office details 

    <MyURL>/MyDetails?Id=123  - This should result all the data same as above

    <MyURL>/MyDetails?Id=123 & Mode="Home" - This should result only home details 

    <MyURL>/MyDetails?Id=123 & Mode="Office" - This should result only Office details 

    Regards,

    Achal



    Achal Kumar

    Monday, October 9, 2017 9:18 AM

Answers

  • I have to use use alternate param along with optional params in my WCF Rest API
    My API looks like

     [OperationContract]
     [WebGet(UriTemplate = "/Details", ResponseFormat = WebMessageFormat.Xml)]
      string GetDetails();

    Alternate paramas URL giving back the same results are :

    <MyURL>/MyDetails/?Name=Test
    <MyURL>/MyDetails/?Id=123

    If I have to use optional parameters for the requirements like:

    1. Get the details of Name="Test" and Mode="Home" or Mode="Office"

    2. Get the details of Id="123" and Mode="Home" or Mode="Office"

    Both 1&2 should return the same - In case of no Mode Input all the details (Office + Home) returns as list. In order to get specific data pass Mode="Home".

    Any suggestion to handle such situation in WCF Rest


    Achal Kumar

    Monday, October 9, 2017 10:59 AM

All replies

  • I have to use use alternate param along with optional params in my WCF Rest API
    My API looks like

     [OperationContract]
     [WebGet(UriTemplate = "/Details", ResponseFormat = WebMessageFormat.Xml)]
      string GetDetails();

    Alternate paramas URL giving back the same results are :

    <MyURL>/MyDetails/?Name=Test
    <MyURL>/MyDetails/?Id=123

    If I have to use optional parameters for the requirements like:

    1. Get the details of Name="Test" and Mode="Home" or Mode="Office"

    2. Get the details of Id="123" and Mode="Home" or Mode="Office"

    Both 1&2 should return the same - In case of no Mode Input all the details (Office + Home) returns as list. In order to get specific data pass Mode="Home".

    Any suggestion to handle such situation in WCF Rest


    Achal Kumar

    Monday, October 9, 2017 10:59 AM
  • Hi Achal,

    For this requirement, you could try to get Name, Id and Mode by “HttpUtility.ParseQueryString(URL)”, and then check what their values are.

    I suggest you make a test with below code.

            public string GetDetails()
            {
                string URL=WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.Query;
                string name = HttpUtility.ParseQueryString(URL).Get("name");
                string id= HttpUtility.ParseQueryString(URL).Get("id");
                string mode = HttpUtility.ParseQueryString(URL).Get("mode");
                if (string.IsNullOrEmpty(mode))
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        return "Name All Result";
                    }
                    else if (!string.IsNullOrEmpty(id))
                    {
                        return "ID All Result";
                    }
                }
                else if (mode.ToUpper() == "HOME")
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        return "Name HOME Result";
                    }
                    else if (!string.IsNullOrEmpty(id))
                    {
                        return "ID HOME Result";
                    }
                }
                else if (mode.ToUpper() == "OFFICE")
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        return "Name Office Result";
                    }
                    else if (!string.IsNullOrEmpty(id))
                    {
                        return "ID Office Result";
                    }
                }
                return "InValid URL";
            }
    

    The requests for all data are like previous, if you want to get Name=Test & Mode=Home, you could try below URL.

    http://localhost:1818/Service1.svc/Details?name=test&mode=office

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 10, 2017 1:41 AM