QueryStringConverter, , . , .
1. QueryStringConverter, :
public class CustomQueryStringConverter : System.ServiceModel.Dispatcher.QueryStringConverter
{
public override bool CanConvert(Type type)
{
if (type == typeof(Dictionary<string, string>))
return true;
return base.CanConvert(type);
}
public override object ConvertStringToValue(string parameter, Type parameterType)
{
if (parameterType != typeof(Dictionary<string, string>))
return base.ConvertStringToValue(parameter, parameterType);
if (parameter == null)
return new Dictionary<string, string>();
return JsonConvert.DeserializeObject<Dictionary<string, string>>(parameter);
}
}
2. -, , WebHttpBehavior GetQueryStringConverter, , :
public sealed class CustomWebHttpBehavior : WebHttpBehavior
{
public CustomWebHttpBehavior()
{
DefaultOutgoingResponseFormat = WebMessageFormat.Json;
AutomaticFormatSelectionEnabled = true;
DefaultBodyStyle = WebMessageBodyStyle.Bare;
HelpEnabled = true;
}
protected override System.ServiceModel.Dispatcher.QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
{
return new CustomQueryStringConverter();
}
}
3. - BehaviorExtensionElement, wcf, :
public class CustomWebHttpBehaviorElement : BehaviorExtensionElement
{
public override System.Type BehaviorType
{
get { return typeof(CustomWebHttpBehavior); }
}
protected override object CreateBehavior()
{
return new CustomWebHttpBehavior();
}
}
4. web.config wcf, :
<system.serviceModel>
...
<extensions>
<behaviorExtensions>
<add name="customWebHttpBehaviorElement" type="Your.Namespace.CustomWebHttpBehaviorElement, Your.Assembly" />
</behaviorExtensions>
</extensions>
...
<behaviors>
<endpointBehaviors>
<behavior name="RestServiceEndpointBehavior">
<customWebHttpBehaviorElement />
</behavior>
</endpointBehaviors>
</behaviors>
...
</system.serviceModel>
( ) , RestServiceEndpointBehavior , .
:. , json : /SomeResource/?args={"a":1,"b"=2,"c"=3}, , 1 (args), (a, b, c).
2:
:
public string DoTheThing(Dictionary<string, string> args)
{
var resourceName = "";
if(args.ContainsKey("resourceName"))
resourceName = args["resourceName"];
return string.Format("You entered: {0}", args);
}
:
[WebInvoke(Method = "GET")]
[OperationContract]
string DoTheThing(Dictionary<string, string> args);
, "args" , .
, UriTemplate, , json, , , .
, .