Optional query string parameters in URITemplate in WCF?

前端 未结 5 1754
盖世英雄少女心
盖世英雄少女心 2020-12-17 07:55

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

[OperationContract]
    [WebGet(UriTemplate = \"Test?format=XML&records={r         


        
5条回答
  •  再見小時候
    2020-12-17 08:10

    While this is an old question, we still come to this scenario from time to time in recent projects.

    To send optional query parameters, I created WCF Web Extensions nuget package.

    After installation, you can use the package like this:

    using (var factory = new WebChannelFactory(new WebHttpBinding()))
    {
        factory.Endpoint.Address = new EndpointAddress(ServiceUri);
        factory.Endpoint.EndpointBehaviors.Add(new QueryParametersServiceBehavior());
        using (var client = factory.CreateWebChannel())
        {
            client.AddQueryParameter("format", "xml");
            client.AddQueryParameter("version", "2");
            var result = client.Channel.GetReport();
        }
    }
    

    Server side you can retrieve the parameters using WebOperationContext:

    WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters;
    

提交回复
热议问题