ploeh / Hyprlinkr

A URI building helper library for ASP.NET Web API
MIT License
197 stars 34 forks source link

Support for Uri Templates in v2? #27

Closed pmcevoy closed 10 years ago

pmcevoy commented 10 years ago

I realize that features for hyprlinkr are now fixed, so perhaps you could consider adding support for generating Uri Templates (ala http://tools.ietf.org/html/rfc6570) in v-next?

ploeh commented 10 years ago

I usually do this:

Assume a SearchController with a Get method:

public HttpResponseMessage Get(string q)

In order to create a template from this, I do this:

Href = this.linker.GetUri<SearchController>(c =>
    c.Get("{query}")).ToString()

This automatically produces a URL like http://foo.com/search?q={query}.

Would that be usable, or do you need something more than that?

pmcevoy commented 10 years ago

I can certainly give it a go and see how far it gets me... although that looks like all controller method params would then need to be "string".

ploeh commented 10 years ago

Yes, but that's just an honest object model; URLs are strings, too ;)

ploeh commented 10 years ago

Does this answer your question?

pmcevoy commented 10 years ago

Sorry for not closing. Would be great if it was able to introspect and auto generate, but for now this digs me out of a hole...

andresmoschini commented 9 years ago

As I commented in #37, I think that it is very useful to accept typed parameters, here you have a quick and dirty implementation:

https://github.com/andresmoschini/hypermedia-api-poc/commit/3381397fab6375064e0dd18659cc90f917a11af6

The result is the follow:

{
  "links": [
    {
      "href": "/",
      "rel": "self home"
    },
    {
      "href": "/accounts",
      "rel": "account-collection",
      "description": "Account List"
    },
    {
      "href": "/accounts/128",
      "rel": "account-item",
      "description": "My account details"
    },
    {
      "href": "/accounts/{accountId}",
      "rel": "account-item",
      "description": "Other accounts"
    },
    {
      "href": "/accounts/{miPersonalizedText}",
      "rel": "account-item",
      "description": "Other accounts"
    }
  ]
}