Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The way I do this currently is I pass back a response dto which implements an interface</p> <pre><code>public interface IHaveLinks { [IgnoreDataMember] IEnumerable&lt;Link&gt; Links { get; } } public class Link { public string Name { get; set; } public IReturn Request { get; set; } public string Method { get; set; } } </code></pre> <p>Then I use a response filter to generate the urls and populate the response headers with the links.</p> <pre><code>this.ResponseFilters.Add((req, res, dto) =&gt; { if (!(dto is IHaveLinks)) return; var links = (dto as IHaveLinks).Links if(links == null || !links.Any()) return; var linksText = links .Select(x =&gt; string.Format("&lt;{0}&gt;; rel={1}"), x.Request.ToUrl(x.Method), x.Name)); var linkHeader = string.Join(", ", linksText); res.AddHeader("Link", linkHeader); }); </code></pre> <p>This seems the cleanest way. The <code>Link</code> object above effectively says "If you make this request with this method you will get back the named resource". The only HTTP thing that bleeds up to the BLL is <code>Method</code>. But you could get rid of that and only pass back GET urls. Or map it to some generalised "operation"?</p> <p>As an example:</p> <pre><code>public class ExampleService : Service { public ExamplesResponse Get(ExamplesRequest request) { var page = request.Page; var data = // get data; return new ExamplesResponse { Examples = data, Links = new [] { new Link { Name = "next", Request = request.AddPage(1), Method = "GET" }, new Link { Name = "previous", Request = request.AddPage(-1), Method = "GET" }, } } } } [Route("/examples/{Page}")] public class ExamplesRequest : IReturn&lt;ExamplesResponse&gt; { public int Page { get; set; } // ... } </code></pre> <p>(The <code>AddPage</code> method returns a clone of the request and sets the Page property appropriately.)</p> <p>Hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload