zoukankan      html  css  js  c++  java
  • ASP.NET MVC 提供与訪问 Web Api

    ASP.NET MVC 提供与訪问 Web Api

    一、提供一个 Web Api
    新建一个项目。类型就选 “Web Api”。

    我用的是MVC5,结果生成的项目一大堆东西。还编译只是,真操蛋。用nuget装了好一阵才跑通。我预计MVC Web项目也能够的,甚至Web Form应该都行。

    以下是一个Action。

    主要是想返回json数据。

    public ContentResult GetUser()
            {
                return new ContentResult {
                    ContentEncoding = Encoding.GetEncoding("utf-8")
                    ,ContentType = "application/json"
                    ,Content = @"{
      ""code"" : ""0",
      ""desc"" : ""请求数据成功!",
      ""dataobj"" : [ {
        ""action"" : ""I",
        ""userCode"" : ""001",
        ""userName"" : ""张三""
      }, {
        ""action"" : ""U",
        ""userCode"" : ""002",
        ""userName"" : ""李四""
      } ]}" };
            }
        返回类型用JsonResult也能够,但无法应用预先拼好的json字符串。
    

    二、訪问Web Api

        //url = "http://192.168.0.179/WebApi/Data/GetUser"
            protected static string getJson(string url)
            {
                string json;
                using (WebClient webClient = new WebClient())
                {
                    webClient.Encoding = Encoding.GetEncoding("utf-8");
                    json = webClient.DownloadString(url);
                }
                return json;
            }
  • 相关阅读:
    路径问题
    移动端推荐使用
    js获取各种宽高方法
    html 符号大全
    bzoj4923 K小值查询
    bzoj3781 小B的询问
    bzoj1799 [Ahoi2009]self 同类分布
    bzoj2005 [Noi2010]能量采集
    bzoj4039 集会
    bzoj2516 电梯
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7119369.html
Copyright © 2011-2022 走看看