Compartir a través de


ADO.NET Data Services support for $value

Today's entry is about a feature that allows ADO.NET Data Services to play very nicely with other software that runs on the Internet, like browsers or any program that can take URLs in fact.

Being REST-based, communication with an ADO.NET Data Service is made in terms of resources, like /Customers('ALFKI'), /Orders(123)/ShippingDate or /Regions?$top=10. We have different representations for these (ATOM, XML and JSON), which programs can interpret and process.

For certain resources, however,  we can also provide a representation based on the "underlying value" rather than a more structured representation for it. Enter the $value suffix, which I've talked about in the past at https://blogs.msdn.com/marcelolr/archive/2008/01/21/service-operations-in-ado-net-data-services.aspx.

Let's say that customers have a "CompanyName" property. A URL such as service.svc/Customers('ALFKI')/CompanyName would refer to a resource that is an XML document with a single element called CompanyName, enclosing the value. The service will also accept service.svc/Customers('ALFKI')/CompanyName/$value, in which case the representation for the resource is just the value for the company name, with a MIME type of text/plain.

How do we pick the MIME type? By default, everything that is typically converted to text (most primitive values) are converted to text/plain. Values which are better suited to a binary representaiton (in particular, byte arrays), use application/octet-stream instead.

When the MimeTypeAttribute is placed on a property (or in the CSDL model for EDM-based data services), the MIME will be the one specified by the attribute. This is always controlled by the server, so a malicious client cannot upload a value, tag it as a Javascript file, and then redirect others that may trust your server to it, for example.

So if you have a property that is actually an image in your database, or perhaps an XML document, you can place the correct attribute on it and then direct a browser to it with an <img src=.... /> element, for example... The building blocks are open - I'm sure folks will come up with more interesting uses for this than I can think of right now!