zoukankan      html  css  js  c++  java
  • Nancy 返回值详解

    简介

    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<TModel>(this IResponseFormatter formatter, TModel model)
        {
            return new JsonResponse<TModel>(model);
        }
    
        public static Response AsXml<TModel>(this IResponseFormatter formatter, TModel model)
        {
            return new XmlResponse<TModel>(model);
        }
    
        public static Response Image(this IResponseFormatter formatter, string imagePath)
        {
            return new ImageResponse(imagePath);
        }
    }
    public static Action<Stream> 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

  • 相关阅读:
    烟花散尽漫说无(參考资料)
    [Leetcode]-Majority Element
    百度移动搜索測试电面
    可删除超炫&amp;多种特效的Card视图(改造自cardsui-for-android开源项目),提供DEMO下载
    关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
    Android Snackbar使用方法及小技巧-design
    Android Intent Action 大全
    android权限大全
    Android 判断SD卡是否存在及容量查询
    Android中蓝牙的基本使用----BluetoothAdapter类简介
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/7116540.html
Copyright © 2011-2022 走看看