OWIN中间件无法设置http状态代码

时间:2019-03-22 17:51:19

标签: asp.net asp.net-web-api2 owin owin-middleware

我仅使用一个中间件运行WebAPI,但无法设置响应HTTP状态代码。

我正在使用OnSendingHeaders()并能够添加标头和设置响应主体,但是状态代码未设置,并且响应始终将其设置为200 OK。

我可以在ValidateUrl(context)中设置响应状态代码。区别在于ValidateUrl(context)是同步调用的,而OnSendingHeaders()将在ProcessIncomingRequest()执行之后异步调用。

是否在调用OnSendingHeaders()之前就发送了HTTP状态行?

异步处理传入请求时,如何/在哪里设置响应HTTP状态代码?

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        appBuilder.Use(typeof(SampleMiddleware));
    }
}

public class SampleMiddleware : OwinMiddleware
{
    private string _responseBody = null;

    public SampleMiddleware(OwinMiddleware next) : base(next)
    {
    }

    public async override Task Invoke(IOwinContext context)
    {

        if (!ValidateUrl(context))
        {
            return;
        }

        context.Response.OnSendingHeaders(state =>
        {
            var cntxt = (IOwinContext)state;
            SetResponseMessage(cntxt);
        }, context);

        await ProcessIncomingRequest();

        await Next.Invoke(context);
    }

    private void SetResponseMessage(IOwinContext context)
    {
        //Setting status code
        context.Response.StatusCode = 201;

        //Setting headers
        context.Response.Headers.Add("XYZ", new[] { "Value-1" });
        context.Response.Headers.Add("ABC", new[] { "Value-2" });

        //Setting response body
        context.Response.Write(_responseBody);
    }

    private async Task ProcessIncomingRequest()
    {
        // Process request
        //await ProcessingFunc();

        // Set response body
        _responseBody = "Response body based on above processing";
    }

    private bool ValidateUrl(IOwinContext context)
    {
        if (context.Request.Host.ToString().Equals("xyx"))
        {
            return true;
        }
        else
        {
            context.Response.StatusCode = 400;
            context.Response.Write("Bad Request");
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

有人立即回复了我的上述帖子,并建议在“等待ProcessIncomingRequest();”之后移动OnSendingHeaders。建议有效!谢谢! 奇怪的是,该答复不再可见。

现在我要解决另一个问题。当我在响应中添加Content-Length标头时,响应代码未设置,默认为200 OK。有什么线索吗?

public async override Task Invoke(IOwinContext context)
{
    if (!ValidateUrl(context))
    {
        return;
    }

    await ProcessIncomingRequest();

    context.Response.OnSendingHeaders(state =>
    {
        var cntxt = (IOwinContext)state;
        SetResponseMessage(cntxt);
    }, context);
}

private void SetResponseMessage(IOwinContext context)
{
    //Setting status code
    context.Response.StatusCode = 201;

    //Setting headers
    context.Response.Headers.Add("XYZ", new[] { "Value-1" });
    context.Response.Headers.Add("ABC", new[] { "Value-2" });

    //Status code is not getting set and default to 200 OK when I add Content-Length header
    context.Response.Headers.Add("Content-Length", new[] { string.Format("{0}", _responseBody.Length) });

    //Setting response body
    context.Response.Write(_responseBody);
}