首页 > Windows开发 > 详细

C# webapi

时间:2019-11-01 20:39:33      阅读:83      评论:0      收藏:0      [点我收藏+]
        HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
            HttpRequestBase request = context.Request;//定义传统request对象
            var t = request.Form["sets"];

 

  //返回字符串
        //请求方式为 http://localhost:4243/Home/Index0?$format=json
        [HttpGet]

        public string Index0()
        {
            return "value1";
        }
        //返回json对象
        [HttpGet]

        public IEnumerable<string> Index00()
        {
            return new string[] { "value1", "value2" };
        }
        [HttpGet]
        public string[] Index1()
        {
            return new string[] { "value1", "value2" };
        }
        [HttpGet]
        public HttpResponseMessage Index2()
        {
            string json = string.Format("[{{ \"re1\":{0},\"re2\":{0},\"re3\":{0} }},{{ \"re1\":{0},\"re2\":{0},\"re3\":{0} }}]",string.Empty);// "{\"result\":\"jsons\"}";
            return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
        }
            // Web API 配置和服务
            config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
            config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");
            // Web API 路由
            config.MapHttpAttributeRoutes();

 

C# webapi

原文:https://www.cnblogs.com/enych/p/11779064.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!