I have an issue with my browser failing to recognise the content types I am sending in my responses and trying to download the file instead of displaying it.
I have a generic handler (named SPARQL.ashx) written in ASP.Net which does some work and produces an object which is of two possible types. Either it gets a SPARQLResultSet or a Graph and it then sets the appropriate content type before using the appropriate Save method to send the content to the user. Code fragment is below:
//Execute the Query
Object result = store.ExecuteQuery(sparqlquery);
if (result is SPARQLResultSet)
{
//Return as SPARQL Results XML Format
context.Response.ContentType = MIMETypesHelper.SPARQL[0];
SPARQLResultSet resultset = (SPARQLResultSet)result;
resultset.Save(new StreamWriter(context.Response.OutputStream));
}
else if (result is Graph)
{
//Return as Turtle
context.Response.ContentType = MIMETypesHelper.Turtle[0];
Graph g = (Graph)result;
TurtleWriter ttlwriter = new TurtleWriter();
ttlwriter.PrettyPrintMode = true;
ttlwriter.Save(g, new StreamWriter(context.Response.OutputStream));
}
My issue is that my browser will often prompt to download the results rather than displaying them despite the fact that one format is XML based and the other plain text based and so should both be displayable in any modern browser.
Behaviour varies from browser to browser and some will prompt for download regardless of result format and some will for one but not the other.
Am I likely to need to configure IIS somehow to ensure the correct MIME types are sent. For the record I have the official file extensions and MIME types registered in IIS. Or is this an issue with the fact that I'm using a Generic Handler? Or does anyone have any other ideas why this might be happening?
Edit
Added code from MIMETypesHelper class for clarity
/// <summary>
/// Helper Class containing arrays of MIME Types for the various RDF Concrete Syntaxes
/// </summary>
/// <remarks>The first type in each array is the canonical type that should be used</remarks>
public class MIMETypesHelper
{
/// <summary>
/// MIME Types for Turtle
/// </summary>
public static string[] Turtle = { "text/turtle", "application/x-turtle", "application/turtle" };
/// <summary>
/// MIME Types for RDF/XML
/// </summary>
public static string[] RDFXML = { "application/rdf+xml" };
/// <summary>
/// MIME Types for Notation 3
/// </summary>
public static string[] Notation3 = { "text/n3", "text/rdf+n3" };
/// <summary>
/// MIME Types for NTriples
/// </summary>
public static string[] NTriples = { "text/plain" };
/// <summary>
/// MIME Types for SPARQL Result Sets
/// </summary>
public static string[] SPARQL = { "application/sparql-results+xml" };
///etc.
}