Enable SPARQL query in your MVC3 application

BrightstarDB uses SPARQL as its primary query language. Because of this and because all the entities you create with the BrightstarDB entity framework are RDF resources, it is possible to turn your application into a part of the Linked Data web with just a few lines of code. The easiest way to achieve this is to add a controller for running SPARQL queries.

As shown in the code above, all you need to do in the code is create a client instance (when using no parameters like this, the connection string is read from your web.config file). The client you get back is actually a BrightstarDB service client and is not tied to the store specified in your connection string, so you need to provide the store name as a parameter to ExecuteQuery (in this case “NerdDinner”). The result of the ExecuteQuery method is a stream containing the SPARQL results in XML format, so all you need to do is wrap this stream in a FileStreamResult and set an appropriate media type. The [ValidateInput(false)] decoration is required because SPARQL queries contain characters such as < and > which ASP.NET considers harmful, if you omit this attribute you will get an exception thrown from ASP.NET.

Now you can query the SPARQL endpoint like this:

http://localhost:49608/sparql?query=SELECT ?d ?o WHERE {?d a <http://brightstardb.com/namespaces/default/Dinner>}