博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nancy 返回值详解
阅读量:6907 次
发布时间:2019-06-27

本文共 2895 字,大约阅读时间需要 9 分钟。

原文:

简介

Nancy 是一个轻量级的,简单粗暴的framework用来构建基于HTTP的各种服务,兼容.Net和Mono。它的返回值也是多种多样的,适应各种不同的情况。包括Response.AsFile()、Response.AsRedirect()、 Response.AsImage()、 Response.AsJson()、Response.AsText()、 Response.AsXml()等

一:string

Get["/get"] = parameters =>{   return "Nancy";}

二:返回视图,像MVC一样,需要有Views/Home/index.html网页才能成功

Get["/get"] = parameters => { return View["/home/index.html"]; }

 

 

三:Response

Post["/GetMore"] = p =>{  Product pd = new Product();  pd.Id = 10;  pd.Address = "北京超越";  pd.Name = "苹果手机";  pd.Price = 1000; return Response.AsJson(pd);  return Response.AsXml(pd); }

四:Response返回值源码

public static implicit operator Response(HttpStatusCode statusCode)    {        return new Response { StatusCode = statusCode };    }    public static implicit operator Response(int statusCode)    {        return new Response { StatusCode = (HttpStatusCode)statusCode };    }    public static implicit operator Response(string contents)    {        return new Response { Contents = contents, ContentType = "text/html", StatusCode = HttpStatusCode.OK };    }    public static implicit operator string(Response response)    {        return response.Contents;    }

 五:Contents源码

public static class FormatterExtensions{    public static Response AsJson
(this IResponseFormatter formatter, TModel model) { return new JsonResponse
(model); } public static Response AsXml
(this IResponseFormatter formatter, TModel model) { return new XmlResponse
(model); } public static Response Image(this IResponseFormatter formatter, string imagePath) { return new ImageResponse(imagePath); }}

 

public static Action
Static(this IViewEngine engine, string virtualPath) { return stream => { var path = HostingEnvironment.MapPath(virtualPath); using (var reader = new StreamReader(path)) { using(var writer = new StreamWriter(stream)) { writer.Write(reader.ReadToEnd()); writer.Flush(); } } }; },

六、自定义返回值

Get["/get"] = parameters =>            {                var path = AppDomain.CurrentDomain.BaseDirectory + "/Views/home/index.html";                Response response = new Response();                response.ContentType = "text/html";                response.Contents = stream =>                {                    using (var reader = new StreamReader(path))                    {                        using (var writer = new StreamWriter(stream))                        {                            writer.Write(reader.ReadToEnd());                            writer.Flush();                        }                    }                };                return response;            };

 

 

参考文章:http://www.cnblogs.com/bnbqian/p/4944829.html

你可能感兴趣的文章
Linux命令行安装weblogic
查看>>
[雪峰磁针石博客]python库介绍-图像处理工具pillow中文文档-手册(2018 5.*)
查看>>
Android Studio添加第三方库
查看>>
python:函数的高级特性
查看>>
iptsbles系列一
查看>>
Ubuntu 16.04系统开机紫屏的解决办法
查看>>
《Java8实战》-第五章读书笔记(使用流Stream-02)
查看>>
安卓运动圆环自定义View
查看>>
启用 Spring Data JPA 审计功能
查看>>
@ConditionalOnMissingBean注解使用
查看>>
初识 JSP---(Session机制)
查看>>
白屏化背后,DBA应有的数据库自动化建设思路
查看>>
【MySQL疑难杂症】如何将树形结构存储在数据库中(方案一 Adjacency List)
查看>>
净流入率第一!工程师最爱一路向“杭”
查看>>
vs2010中工具箱不显示DevExpress控件的解决办法
查看>>
SSM-SpringMVC-09:SpringMVC中以继承MutiActionController类的方式实现处理器
查看>>
SVG滤镜对图片调色
查看>>
内容、平台和硬件,哪个方面更值得VR大规模投入?
查看>>
苹果第三财季派息32亿美元 回购100亿美元股票
查看>>
Hibernate ORM 5.3.9.Final 发布,常规更新版本
查看>>