开发者

Parsing hierarchical URLs

开发者 https://www.devze.com 2023-01-10 19:13 出处:网络
I\'m new to REST and I\'m building a service using the WCF REST Starter Kit Preview 2 in C#. Most examples showing how to define a UriTemplate assume that you know the exact format of the URL an开发者

I'm new to REST and I'm building a service using the WCF REST Starter Kit Preview 2 in C#. Most examples showing how to define a UriTemplate assume that you know the exact format of the URL an开发者_如何学运维d can pick out the bits you need to perform the request. But...

How do I allow users to enter a URL that defines a hierarchy and how do I process it? e.g. say I want to provide an online file-storage facility that allows user to view the contents of "folders" (all served from a database - not physical folders of course)

http://mysite.com/MyService/Folder/root/level1/level2/level3

i.e. the user wants to list the contents of a "Folder" that is specified as "root/level1/level2/level3". I can then take this path and serve data from my DB based on this info.

Thanks!


You can use a wildcard (*) in your UriTemplate. For example:

[WebGet(UriTemplate="Folder/{*path}")]
public List<Files> GetStuff(string path) {
  //path is 'root/level1/level2/level3', which you can then parse
}

Here's a link on MSDN that provides a description and the rules around UriTemplates (rules are about 1/2 way down). The main thing to keep in mind is that you can only have one wildcard segment for a template string. Hopefully this helps!

BTW, the link is to MSDN docs for .NET 4. I think this also applies to 3.5.

0

精彩评论

暂无评论...
验证码 换一张
取 消