浏览器无法正确识别内容类型标题
-
20-08-2019 - |
题
我有我的浏览器没有认识到我在我的答复发送的内容类型,并尝试下载文件,而不是显示它的一个问题。
我有一个通用的处理程序(名为SPARQL.ashx)写在ASP.Net其中做了一些工作,并产生一个对象,它是两种可能类型的。它要么得到一个SPARQLResultSet或图形,然后使用适当的保存方法来发送内容给用户之前将适当的内容类型。代码片段是以下:
//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));
}
我的问题是,我的浏览器经常会提示下载结果,而不是尽管一个格式是基于XML和基于其他纯文本等等都应该在任何现代浏览器中显示的显示它们。
行为变化从浏览器的浏览器和一些将提示下载不管结果格式和一些意愿之一,但不是其他。
我会被需要以某种方式配置IIS以确保正确的MIME类型被发送。为了记录我有正式的文件扩展名和IIS注册的MIME类型。或者这是事实,我使用的是通用的处理程序的问题吗?或者有没有人有任何其他想法,为什么这可能发生?
修改强>
这MIMETypesHelper类添加的代码,为了清楚
/// <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.
}
解决方案 4
终于找到了问题的根源,似乎你必须调用的响应清除(),并使用缓冲或内容类型标题预期不会发送:
例如
//Return as Turtle
context.Response.ContentType = MIMETypesHelper.Turtle[0];
//Clear any other output from this Response
//Then use buffering
context.Response.Clear();
context.Response.BufferOutput = true;
//Write the Output
Graph g = (Graph)result;
TurtleWriter ttlwriter = new TurtleWriter();
ttlwriter.PrettyPrintMode = true;
ttlwriter.Save(g, new StreamWriter(context.Response.OutputStream));
其他提示
这是你的代码看起来你是依托于RDF库(你不说哪一个)的MIME类型。当浏览器,例如Firefox / IE(你不说,其使用的是)看到一个mime类型application/
SOMETHING 它通常提供给保存它,而不是浏览它。
为RDF / XML MIME类型是application/rdf+xml
(我知道,因为我写规范),并且将导致另存为方法。甲鱼(我创建了龟注)MIME类型是不注册,但是有人建议要text/turtle
哪些应该显示细
什么的内容Dispostion 头设置为?应设置为“内联”。你可能会想尝试将其设置手动,看看是否更改的行为。
在我们的RDF存储引擎的家庭,我们做内容协商攻击这个问题。
也就是说,我们检查在HTTP请求报头中的接受线路,并相应地调整我们的行为。如果接受行提到任何RDF口味,或SPARQL明确结果的MIME类型,那么我们发回相应的内容类型和相应的响应。这在我们的经验,提供专门的客户SPARQL,手卷码和各种RDF“浏览器”的工具非常好。
但是,对于一个典型的Web浏览器的接受行只是列出了HTML和一些图片格式,并且我们的HTTP守护进程设置Content-type为text / plain的(或空白的SPARQL查询,为text / html,与响应主体是与一个形式的网页以从Web浏览器进行测试写入手动查询)
最后,如果接受头部丢失总之,我们知道这是一个很幼稚的软件,最有可能的某人的Perl脚本什么的,我们有一个特殊的情况下,让人们的生活更轻松对存储引擎开发时。