I need to create a custom route for a WCF data service that contains a segment that needs to be retrieved for use in filtering data.
Example:
http://mysample.net/mysamplesvc/client123/Users
I need to extract client123 from the route. It looks like the Route class might provide something like this, but I'm not sure how to implement IRouteHandler for the data service.
Is this the right way? Are there any good examples?
TIA!
UPDATE:
I managed to find the solution that I needed using special rewriting of URLs in IDispatchMessageInspector. The code below is my initial hack and requires a bunch of cleanup. but it seems to work. If anyone sees anything inaudible, let me know.
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext) { HttpRequestMessageProperty httpmsg = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name]; ...Additional logic for handling Query formats in OData UriTemplate template = new UriTemplate("mysamplesvc/{ClientId}", true); Uri prefix = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)); Uri uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri); UriTemplateMatch results = template.Match(prefix, uri); if (results != null && !string.IsNullOrEmpty(results.BoundVariables["ClientId"])) { _clientId = results.BoundVariables["clientId"].ToString(); } if (!string.IsNullOrEmpty(_clientId)) { httpmsg.Headers.Add("ClientId", _clientId); rewriteRequest(); } return null; } private void rewriteRequest() { if (HttpContext.Current != null && HttpContext.Current.Session != null) { if (WebOperationContext.Current.IncomingRequest.UriTemplateMatch != null) { Uri serviceUri = HttpContext.Current.Session["ServiceUri"] as Uri; Uri requestUri = null; UriTemplateMatch match = WebOperationContext.Current.IncomingRequest.UriTemplateMatch; if (serviceUri == null) { UriBuilder serviceUriBuilder = new UriBuilder(match.BaseUri); serviceUri = serviceUriBuilder.Uri; HttpContext.Current.Session["ServiceUri"] = serviceUri; } if (serviceUri != null) { OperationContext.Current.IncomingMessageProperties["MicrosoftDataServicesRootUri"] = serviceUri; UriBuilder requestUriBuilder = new UriBuilder(match.RequestUri); string path = string.Empty; if (match.RelativePathSegments[0] == _clientId) { foreach (var seg in match.RelativePathSegments.Select((x, i) => new { Value = x, Index = i })) { if (seg.Index != 0) { path += "/"; path += seg.Value; } } } else { foreach (var seg in match.RelativePathSegments.Select((x, i) => new { Value = x, Index = i })) { path += "/"; path += seg.Value; } } UriBuilder serviceUriBuilder = new UriBuilder(match.BaseUri + path);
Thanks everyone!
source share