Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WCF
I've implemented a self-hosted local web server using WCF as per this article: Developing Web 2.0 User Interface for Self Hosted WCF Services using HTML5, CSS3 and JQuery
 
In real world applications, I've found that I needed to make the following modifications in order to handle deeper pathing to sub-folders. This works for my current needs because I simply created enough UriTemplates to get 6 folders deep. However, I suspect there must be a more elegant way to handle this that will allow any number of sub-folders to be served.
 
I tried to figure something out with wildcards (e.g. /{*path}/{resource}.{extension}) but it seems wildcards are only permitted in the last segment of the path.
 
Any help would be appreciated,
 
Ken
 
[OperationContract, WebGet(UriTemplate = "/{path}/{resource}.{extension}")]
Stream Links(string path, string resource, string extension);
 
[OperationContract, WebGet(UriTemplate = "/{path1}/{path2}/{resource}.{extension}")]
Stream Links2(string path1, string path2, string resource, string extension);
 
[OperationContract, WebGet(UriTemplate = "/{path1}/{path2}/{path3}/{resource}.{extension}")]
Stream Links3(string path1, string path2, string path3, string resource, string extension);
 
[OperationContract, WebGet(UriTemplate = "/{path1}/{path2}/{path3}/{path4}/{resource}.{extension}")]
Stream Links4(string path1, string path2, string path3, string path4, string resource, string extension);
 
[OperationContract, WebGet(UriTemplate = "/{path1}/{path2}/{path3}/{path4}/{path5}/{resource}.{extension}")]
Stream Links5(string path1, string path2, string path3, string path4, string path5, string resource, string extension);
 
[OperationContract, WebGet(UriTemplate = "/{path1}/{path2}/{path3}/{path4}/{path5}/{path6}/{resource}.{extension}")]
Stream Links6(string path1, string path2, string path3, string path4, string path5, string path6, string resource, string extension);
 
public Stream Links(string path, string resource, string extension) {
    return ...;
}
 
public Stream Links2(string path1, string path2, string resource, string extension) {
    return Links(Path.Combine(path1, path2), resource, extension);
}
 
public Stream Links3(string path1, string path2, string path3, string resource, string extension) {
    return Links(Path.Combine(path1, path2, path3), resource, extension);
}
 
public Stream Links4(string path1, string path2, string path3, string path4, string resource, string extension) {
    return Links(Path.Combine(path1, path2, path3, path4), resource, extension);
}
 
public Stream Links5(string path1, string path2, string path3, string path4, string path5, string resource, string extension) {
    return Links(Path.Combine(path1, path2, path3, path4, path5), resource, extension);
}
 
public Stream Links6(string path1, string path2, string path3, string path4, string path5, string path6, string resource, string extension) {
    return Links(Path.Combine(path1, path2, path3, path4, path5, path6), resource, extension);
}
Posted 25-Jul-11 4:03am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why not just use something like this...
 
        [OperationContract]
        [WebGet(UriTemplate = "{*pathname}")]
        public Stream Links(string pathname)
        {
            string path = Path.GetDirectoryName(pathname);
            string resource = Path.GetFileNameWithoutExtension(pathname);
            string extension = Path.GetExtension(pathname).TrimStart('.');
            return Links(path, resource, extension);
        }
  Permalink  
v7
Comments
lxq007 at 19-Mar-14 13:15pm
   
I use [WebGet(UriTemplate = "{*pathname}")] test myself.
 
And the url='http://localhost:50923/OMSService.svc/Links?seller=s001&service=s2&timstamp=324&user=uo&pass=009',
 
and then why the pathname='Links'.It means that the pathname is the called method name ,
 
is not the long string 'seller=s001&service=s2&timstamp=324&user=uo&pass=009'!
 
Thanks in advance !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 604
1 thatraja 370
2 OriginalGriff 319
3 Sergey Alexandrovich Kryukov 241
4 kbrandwijk 204
0 OriginalGriff 737
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 505
3 Sergey Alexandrovich Kryukov 395
4 thatraja 370


Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 25 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100