在南希中回复正文和状态码 [英] Respond with both body and status code in Nancy

查看:84
本文介绍了在南希中回复正文和状态码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Nancy的新手,我想同时返回自定义HttpStatusCode和正文(内容).如果我返回HttpStatusCode,它将以空白正文返回.如果我返回一个字符串,则它将返回它作为正文,但始终返回200状态代码为OK.

I'm new to Nancy and I want to return both a custom HttpStatusCode and a body (content). If I return an HttpStatusCode, it returns it with a blank body. If I return a string then it returns that as the body but always with a 200 status code of OK.

public class SendSMS : NancyModule
{
    public SendSMS()
    {
        Post["/SendSMS"] = parameters =>
            {
                return HttpStatusCode.BadRequest; // this works, no body
                return "Missing \"to\" parameter"; // this works, 200 status code
                // want to return status code with message
            };
    }
}

推荐答案

您始终可以创建Response类型的实例,并自行设置BodyStatusCode.如果您想采取捷径,可以执行类似的操作

You could always create an instance of the Response type and set the Body and StatusCode yourself. If you wanted to take a shortcut you could do something like

var r = (Response)"Some string that goes into the body";
r.StatusCode = 123;

return r;

这篇关于在南希中回复正文和状态码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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