Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional UriTemplate parameter using WebGet

Tags:

rest

c#

wcf

I have tried these

Optional Parameters in WCF Service URI Template? Posted by Kamal Rawat in Blogs | .NET 4.5 on Sep 04, 2012 This section shows how we can pass optional parameters in WCF Servuce URI inShare

and

Optional query string parameters in URITemplate in WCF

But nothing works for me. Here is my code:

    [WebGet(UriTemplate = "RetrieveUserInformation/{hash}/{app}")]
    public string RetrieveUserInformation(string hash, string app)
    {

    }

It works if the parameters are filled up:

https://127.0.0.1/Case/Rest/Qr/RetrieveUserInformation/djJUd9879Hf8df/Apple  

But doesn't work if app has no value

https://127.0.0.1/Case/Rest/Qr/RetrieveUserInformation/djJUd9879Hf8df  

I want to make app optional. How to achieve this?
Here is the error when app has no value:

Endpoint not found. Please see the service help page for constructing valid requests to the service.  
like image 339
fiberOptics Avatar asked Mar 08 '13 07:03

fiberOptics


2 Answers

You have two options for this scenario. You can either use a wildcard (*) in the {app} parameter, which means "the rest of the URI"; or you can give a default value to the {app} part, which will be used if it's not present.

You can see more information about the URI Templates at http://msdn.microsoft.com/en-us/library/bb675245.aspx, and the code below shows both alternatives.

public class StackOverflow_15289120
{
    [ServiceContract]
    public class Service
    {
        [WebGet(UriTemplate = "RetrieveUserInformation/{hash}/{*app}")]
        public string RetrieveUserInformation(string hash, string app)
        {
            return hash + " - " + app;
        }
        [WebGet(UriTemplate = "RetrieveUserInformation2/{hash}/{app=default}")]
        public string RetrieveUserInformation2(string hash, string app)
        {
            return hash + " - " + app;
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        Console.WriteLine(c.DownloadString(baseAddress + "/RetrieveUserInformation/dsakldasda/Apple"));
        Console.WriteLine();

        c = new WebClient();
        Console.WriteLine(c.DownloadString(baseAddress + "/RetrieveUserInformation/dsakldasda"));
        Console.WriteLine();

        c = new WebClient();
        Console.WriteLine(c.DownloadString(baseAddress + "/RetrieveUserInformation2/dsakldasda"));
        Console.WriteLine();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
like image 100
carlosfigueira Avatar answered Nov 08 '22 10:11

carlosfigueira


A complementary answer regarding default values in UriTemplates using query parameters. The solution proposed by @carlosfigueira works only for path segment variables according to the docs.

Only path segment variables are allowed to have default values. Query string variables, compound segment variables, and named wildcard variables are not permitted to have default values.

like image 44
Bahaa Avatar answered Nov 08 '22 09:11

Bahaa