Is there a way to validated the UriTemplate in my REST service? RRS feed

  • Question

  • I have this REST service:

       [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "LoadPostings/?OriginCode={OriginCode}&DestinationCode={DestinationCode}&EquipmentTypeId={EquipmentTypeId}&IsTruckLoad={IsTruckLoad}&SessionId={SessionTokenId}")]
        SupplierLoadSearchResults GetLoadPostings(string OriginCode, string DestinationCode, int EquipmentTypeId, string IsTruckLoad, string SessionTokenId);

    This is the UriTemplate:

    UriTemplate = "LoadPostings/?OriginCode={OriginCode}&DestinationCode={DestinationCode}&EquipmentTypeId={EquipmentTypeId}&IsTruckLoad={IsTruckLoad}&SessionId={SessionTokenId}"

    I noticed that when the Incoming Request changes the name of the resource for example:

    Change EquipmentTypeId to something like EquipmentId

    .NET does not give me an error on that, in face what does happen is the I get a 0 value (because in this case the EquipmentTypeId is an int) And that 0 is being passed to my database layer and breaks there.

    Is there a way to validate the UriTemplate in .net ?

    I wrote down this code to compare the keys between teh UriTempalte and what the user sends me, but I wonder if there is a better way to validate the UriTemplate?

    NameValueCollection incomingQueryParams = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters; System.Collections.ObjectModel.ReadOnlyCollection<String> templateQueryParams = WebOperationContext.Current.GetUriTemplate("GetLoadPostings").QueryValueVariableNames;


    Friday, May 3, 2013 2:58 PM