[编辑] **问题似乎是我们机器上的某些东西正在拦截通过端口 8888 发送的 HTML——更改端口号似乎可以解决问题。可能在 8888 端口上运行的东西仍然是个谜。**
我的 Owin 处理程序中有一个最奇怪的错误,它返回 HTML 响应。我已将其简化为:
internal static Task WebUIHandler(IOwinRequest request, IOwinResponse response)
{
response.ContentType = "text/html";
var html = ... build an HTML string ...;
response.Headers["Content-Encoding"] = "gzip";
response.Write(GZip.CompressUTF8(html));
return Task.FromResult(0);
}
该GZip.CompressUTF8
调用是对将html
字符串压缩为字节数组的实用方法(是的,我知道这可以通过流来完成,但这不是重点)。
当我运行这个应用程序时,它所服务的页面似乎是未压缩的(即,纯文本,未压缩)并且Content-Encoding
标题不存在(我在 Fiddler4、Chrome 和 Firefox 中检查过这个)。
但是,如果我在语句上放置一个断点return
并让应用程序在继续之前只停留几秒钟,那么响应将被压缩并包含Content-Encoding
标题。但前提是我让它在断点上设置几秒钟:如果我继续太快,我就没有 gzip。
谁能解释这里发生了什么?我很迷惑。
[编辑:根据要求,整个“管道”:]
using Microsoft.Owin;
using Microsoft.Owin.Hosting;
using Owin;
...
static void Main(string[] args) {
var startOptions = new StartOptions();
startOptions.Urls.Add(...url...);
using (WebApp.Start(startOptions, AppBuilderAction)) {
...
}
}
...
static void AppBuilderAction(IAppBuilder app) {
var router = new Router();
router.Add("/", WebUIHandler); // See above.
...
app.Run(router.OwinHandler);
}
...
[Router method]
public Task OwinHandler(IOwinContext context) {
var request = context.Request;
var response = context.Response;
... find a handler matching request.Path.Value ...
return handler(request, response);
}
就是这样。除非我误解了 Owin 方案,否则没有其他东西会妨碍您。