码迷,mamicode.com
首页 > Windows程序 > 详细

C# webapi

时间:2019-11-01 20:15:17      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:localhost   val   传统   enc   获取   res   attr   base   dex   

        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

标签:localhost   val   传统   enc   获取   res   attr   base   dex   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!