Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have the same problem with ASP.Net WebAPI, so I don't think this is so much a ServiceStack issue, but just a general concern with dealing with Raven style id's on a REST URL.</p> <p>For example, let's say I query <code>GET: /api/users</code> and return a result like:</p> <pre><code>[{ Id:"users/1", Name:"John" }, { Id:"users/2", Name:"Mary" }] </code></pre> <p>Now I want to get a specific user. If I follow pure REST approach, the Id would be gathered from this document, and then I would pass it in the id part of the url. The problem here is that this ends up looking like <code>GET: /api/users/users/1</code> which is not just confusing, but the slash gets in the way of how WebAPI (and ServiceStack) route url parameters to action methods.</p> <p>The compromise I made was to treat the id as an integer from the URL's perspective only. So the client calls <code>GET: /api/users/1</code>, and I define my method as <code>public User Get(int id)</code>.</p> <p>The cool part is that Raven's <code>session.Load(id)</code> has overloads that take either the full string form, or the integer form, so you don't have to translate most of the time.</p> <p>If you DO find yourself needing to translate the id, you can use this extension method:</p> <pre><code>public static string GetStringIdFor&lt;T&gt;(this IDocumentSession session, int id) { var c = session.Advanced.DocumentStore.Conventions; return c.FindFullDocumentKeyFromNonStringIdentifier(id, typeof (T), false); } </code></pre> <p>Calling it is simple as <code>session.GetStringIdFor&lt;User&gt;(id)</code>. I usually only have to translate manually if I'm doing something with the id other than immediately loading a document.</p> <p>I understand that by translating the ids like this, that I'm breaking some REST purist conventions, but I think this is reasonable given the circumstances. I'd be interested in any alternative approaches anyone comes up with.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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