C# webapi

        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();

 

posted @ 2019-11-01 19:24  enych  阅读(278)  评论(0编辑  收藏  举报