简体   繁体   English

EventSource的响应具有不是“ text / event-stream”的MIME类型(“ text / html”)

[英]EventSource's response has a MIME type (“text/html”) that is not “text/event-stream”

When I try to hook up the EventSource to my controller it keeps saying: 当我尝试将EventSource连接到我的控制器时,它一直在说:

EventSource's response has a MIME type ("text/html") that is not "text/event-stream". EventSource的响应具有不是“ text / event-stream”的MIME类型(“ text / html”)。 Aborting the connection. 中止连接。

I made the following class to help with the handling and preparation of a response. 我开设了以下课程来帮助您处理和准备响应。 In this class I believe I set the responseType accordingly to text/event-stream . 我相信在这一节课中,我将responseType设置为text/event-stream

public class ServerSentEventResult : ActionResult
{
    public delegate string GetContent();
    public GetContent Content { get; set; }
    public int Version { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        if (this.Content != null)
        {
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = "text/event-stream"; response.BufferOutput = false; response.Charset = null;
            string[] newStrings = context.HttpContext.Request.Headers.GetValues("Last-Event-ID");
            if (newStrings == null || newStrings[0] != this.Version.ToString())
            {
                try
                {
                    response.Write("retry:250\n");
                    response.Write(string.Format("id:{0}\n", this.Version));
                    response.Write(string.Format("data:{0}\n\n", this.Content()));
                    response.End();
                }
                catch (HttpException e) { }
            }
            else
            {
                response.Write(String.Empty);
            }
        }
    }
}

Could someone please help me out on this one? 有人可以帮我解决这个问题吗? Thousand times thanks in advance! 一千次提前致谢!

Sorry for the late answer. 抱歉回复晚了。

What I did in my code is prepare my whole response in a string builder and then write it and flush it at once (not 3 writes with an end) 我在我的代码确实是一个字符串生成器准备我的整个响应,然后把它写在刷新一次它(不是3结束写入)

var sb = new StringBuilder(); 
sb.Append("retry: 1\n");
sb.AppendFormat("id: {0}\n", this.Version);
sb.AppendFormat("id: {0}\n\n", this.Content());
Response.ContentType = "text/event-stream";
Response.Write(sb.ToString());
Response.Flush();

on another note, my code is in an MVC controller, so I don't create the response myself (I use this.Response) meaning my header may contains more data than just the ContentType. 另外,我的代码在MVC控制器中,因此我不会自己创建响应(我使用this.Response),这意味着我的标头中可能包含的数据不仅仅是ContentType。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EventSource 的响应具有不是“text/event-stream”的 MIME 类型(“text/plain”)。 中止连接 Azure SignalR - EventSource's response has a MIME type ("text/plain") that is not "text/event-stream". Aborting the connection Azure SignalR 使用 C# 监听文本/事件流 - Listening to text/event-stream using C# 客户发现响应内容类型为“text / html”,但预期为“text / xml” - Client found response content type of 'text/html', but expected 'text/xml' ActionFilter响应类型(text / html)与响应内容不匹配 - ActionFilter response type (text/html) does not match the response content 如何将Linkbutton的文本转换为Click事件中的字符串类型 - how to get the Linkbutton's text to string type in it's Click event Nancy响应未返回index.html的正确MIME类型,包括 - Nancy response not returning proper MIME Type for index.html including 检测没有事件处理程序的情况下TextBox控件的文本是否已更改 - Detect if TextBox control's text has changed without the event handler 任何TextBox的文本发生更改时触发事件处理程序 - Trigger Event Handler when any TextBox's text has changed 得到文本文件的哑剧 - getting the mime of text files Mailkit-在没有完整的mime消息的情况下获取HTML和Text部分 - Mailkit - Get HTML and Text parts without having full mime message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM