0

使用 Owin 中间件进行 304 响应的正确方法是什么?

我正在做这个

public override Task Invoke(IOwinContext context)
{
    var response = context.Response;
    response.ContentType = "application/javascript";
    response.StatusCode = 200;

    if (ClientCached(context.Request, scriptBuildDate))
    {
        response.StatusCode = 304;
        response.Headers["Content-Length"] = "0";
        return null;
    }
    response.Headers["Last-Modified"] = scriptBuildDate.ToUniversalTime().ToString("r");

    return context.Response.WriteAsync(js);
}

返回null对于空体响应是否正确?它可以工作,但我想知道它应该如何设置

4

1 回答 1

1

为了完全符合规范,您应该使用Stream.Null它,因为这是表示无主体场景的理想方式。

public override Task Invoke(IOwinContext context)
{
    var response = context.Response;
    response.ContentType = "application/javascript";

    if (ClientCached(context.Request, scriptBuildDate))
    {
        response.StatusCode = 304;

        //Close the existing body just in case
        response.Body.Close();
        response.Body = Stream.Null;

        return Task.FromResult<Object>(null);
    }
    else
    {
        response.StatusCode = 200;
        response.Headers["Last-Modified"] = scriptBuildDate.ToUniversalTime().ToString("r");

        return context.Response.WriteAsync(js);
    }
}
于 2013-10-29T16:01:23.177 回答