ASP.NET 5 Web API返回状态代码和正文 [英] Asp.net 5 web api return status code and body

查看:213
本文介绍了ASP.NET 5 Web API返回状态代码和正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET 5进行项目开发,并且正在编写网络api.

I'm working on a project using ASP.NET 5 and I'm writing a web api.

我已经继承了一些代码和数据库存储过程,这些过程和数据库存储过程使用了rasrror来指示错误(用户名/密码错误,许可证过期等).

I've inherited some code and database stored procedures that use raiserror to indicate something is wrong (username/password incorrect, expired license etc).

除消息文本外,存储过程不返回任何内容来唯一标识该错误.

The stored procedure returns nothing to uniquely identify that error, except the message text.

我希望能够返回HTTP UNAUTHORIZED响应,同时也将错误消息传递给客户端.

I want to be able to return a HTTP UNAUTHORIZED response, but also shuttle the error message along to the client too.

内置的IActionResult HttpUnauthorized()方法不允许给出原因.

The built in IActionResult HttpUnauthorized() method doesn't allow for a reason to be given.

所以我写了我自己的ActionResult,看起来像这样:

So I wrote my own ActionResult that looks like this:

public class UnauthorizedWithMessageResult : IActionResult
{
    private readonly string _message;

    public UnauthorizedWithMessageResult(string message)
    {
        _message = message;
    }

    public async Task ExecuteResultAsync(ActionContext context)
    {
        using (var sw = new HttpResponseStreamWriter(context.HttpContext.Response.Body, Encoding.UTF8))
        {
            await sw.WriteLineAsync(_message);
        }

        await new HttpUnauthorizedResult().ExecuteResultAsync(context);
    }
}

问题在于客户端会收到200-OK的提示,一切正常.

The problem is that the client is receiving a 200-OK like everything is fine.

我已逐步完成此工作,完成对HttpUnauthorizedResult的委派后,状态代码确实设置为403.

I've stepped through this and after the delegation to HttpUnauthorizedResult has been done, the status code is indeed set to 403.

Web API似乎(在某个时候)看到响应的正文中的内容,并确定这表示一切正常,并重置状态代码.

It looks like Web API is (at some point) seeing that there's content in the body of the response and decides that means everything is OK and resets the status code.

是否有任何方法可以解决此问题而不必诉诸于将邮件作为标头之类的方式发送? (或者这是正确的方法吗?)

Is there any way to get around this without having to resort to sending the message as a header or something? (or is that the correct way to do this?)

推荐答案

它是这样工作的:

public IActionResult IWillNotBeCalled()
{
    return new UnauthorizedWithMessageResult("MY SQL ERROR");            
}

public class UnauthorizedWithMessageResult : IActionResult
{
    private readonly string _message;

    public UnauthorizedWithMessageResult(string message)
    {
        _message = message;
    }

    public async Task ExecuteResultAsync(ActionContext context)
    {
        // you need to do this before setting the body content
        context.HttpContext.Response.StatusCode = 403;

        var myByteArray = Encoding.UTF8.GetBytes(_message);
        await context.HttpContext.Response.Body.WriteAsync(myByteArray, 0, myByteArray.Length);
        await context.HttpContext.Response.Body.FlushAsync();
    }
}

在设置主体之前,必须先设置StatusCode,并且必须冲洗主体流以确保在响应中设置内容.

You have to set the StatusCode before setting the body and you have to flush the body stream to be sure that the content will be set inside the response.

希望它会有所帮助:)

这篇关于ASP.NET 5 Web API返回状态代码和正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆