Respond with both body and status code in Nancy

99封情书 提交于 2019-12-03 01:10:46

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;

This should work.

public class SendSMS : NancyModule
{
   public SendSMS()
   {
       Post["/SendSMS"] = parameters =>
       {
           return Negotiate.WithModel("Missing \"to\" param")
                           .WithStatusCode(HttpStatusCode.BadRequest)           
       };
   }
} 

For more information check the docs on controlling content negotiation.

If you have problems with encoding it's better to use

return new TextResponse(HttpStatusCode.STATUS, "Text Responsé")

This is the simplest way I've found:

Return from your Module:

return new Response {
                StatusCode = HttpStatusCode.NotFound, ReasonPhrase = "Resource not found"
            };
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!