0

I have parameters in my request

[OperationContract]
[WebInvoke(Method = "GET",
  ResponseFormat = WebMessageFormat.Json,
  RequestFormat = WebMessageFormat.Json,
  BodyStyle = WebMessageBodyStyle.Bare,
  UriTemplate = "request?code={codeParam}&name={nameParam}&id={idParam}")]
List<ResponseData> JSONDataList(string codeParam , string nameParam , string idParam);

So I have codeParam, nameParam and idParam.
What if the Uri will be "request?name=myName&BlaBlaParam=example"?

How I can catch this undefined BlaBlaParam param from request in my code?
Service works OK with no exceptions.

Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • The framework routing wouldn't find the proper parameter, so I'd suspect it won't map to the action. More than likely the request would fall to the catch all defined route. To correctly handle, you would more than likely do a [FromBody] and pass your data as an object. So you can sanitize before the server, then sanitize potentially again on the server. In MVC/WebApi anyway. For you, you might want to look at: https://stackoverflow.com/questions/11193356/handling-invalid-uri-passed-to-a-wcf-service – Greg Dec 21 '17 at 16:26

1 Answers1

0

I found solution for this problem. https://books.google.ru/books?id=w02As8L5l7MC&pg=PA29&lpg=PA29&dq=UriTemplateMatch.QueryParameters&source=bl&ots=cXLkc0nU_o&sig=yrsYLZqLB7Im2PrCWZmkGS5kbC4&hl=ru&sa=X&ved=0ahUKEwjlv4aF36TYAhWIBZoKHU64DZwQ6AEIPDAD#v=onepage&q=UriTemplateMatch.QueryParameters&f=false

WebOperationContext webCtx;
webCtx = WebOperationContext.Current;
IncomingWebRequestContext incomingCtx;
incomingCtx = webCtx.IncomingRequest;
string uri;
uri = incomingCtx.UriTemplateMatch.RequestUri.ToString();
NameValueCollection query;
query = incomingCtx.UriTemplateMatch.QueryParameters;
string queryName;
if (query.Count != 0)
{
      var enumQ = query.GetEnumerator();
      while (enumQ.MoveNext())
      {
            queryName = enumQ.Current.ToString();
            Console.Writeline{"{0} = {1}", queryName, query[queryName]);
      }
}

Where query is a dictionary, which contains: keys are names of parameters, values are values of parameters