Alex Rivera | Logout

Optional query string parameters in URITemplate in WCF?

Asked 2010-06-03T19:47:22.160
45

I'm developing some RESTful services in WCF 4.0. I've got a method as below:

[OperationContract]
    [WebGet(UriTemplate = "Test?format=XML&records={records}", ResponseFormat=WebMessageFormat.Xml)]
    public string TestXml(string records)
    {
        return "Hello XML";
    }

So if i navigate my browser to http://localhost:8000/Service/Test?format=XML&records=10, then everything works as exepcted.

HOWEVER, i want to be able to navigate to http://localhost:8000/Service/Test?format=XML and leave off the "&records=10" portion of the URL. But now, I get a service error since the URI doesn't match the expected URI template.

So how do I implement defaults for some of my query string parameters? I want to default the "records" to 10 for instance if that part is left off the query string.

Edit
Report

1 Answer

17

According to this answer this is fixed in .NET 4.0. Failing to supply the query string parameter seems to result in its being given the default value for the type.

answered 2012-07-11T12:58:09.967

Your Answer