基本信息
源码名称:web APi 入门级实例代码下载
源码大小:35.45M
文件格式:.zip
开发语言:C#
更新时间:2016-03-28
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using Models;
using Newtonsoft.Json.Linq;

namespace VCoinWebApi.Controllers
{
    public class UsersController : ApiController
    {
        List<User> users = new List<User>
        { 
            new User { ID = 1, UserName = "Soup1", Platform = Platform.Facebook, Status = "User" },
            new User { ID = 2, UserName = "Soup2", Platform = Platform.Facebook, Status = "User" },
            new User { ID = 3, UserName = "Soup3", Platform = Platform.Facebook, Status = "User" }, 
            new User { ID = 4, UserName = "Soup4", Platform = Platform.Facebook, Status = "User" }, 
        };

        public IEnumerable<User> GetAllUsers()
        {
            return users;
        }

        public User GetUserById(short id)
        {
            var user = users.FirstOrDefault((p) => p.ID == id);
            if (user == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            else
            {
                return user;
            }
        }

        [HttpGet]
        [ActionName("GetUser")]
        public IEnumerable<User> GetUsersByName(string userName)
        {
            return users.Where(
                (p) => string.Equals(p.UserName, userName,
                    StringComparison.OrdinalIgnoreCase));
        }

       
        [HttpPost]
        [ActionName("AddUser")]
        public User Add([FromBody]long id, string userName,int platform,string status)
        {
            User user = new User { ID = id, UserName = userName, Platform = (Platform)platform, Status = status };
            if (user == null)
            {
                throw new HttpRequestException();
            }
            users.Add(user);
            return user;
        }

        [HttpPost]
        [ActionName("AddUser")]
        public User AddUser([FromBody]User user)
        {
            if (user == null)
            {
                throw new HttpRequestException();
            }
            users.Add(user);
            return user;
        }

        [HttpPost]
        public User Delete([FromBody]int id)
        {
            var user = users.FirstOrDefault((p) => p.ID == id);
            if (user == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            else
            {
                users.Remove(user);
                return user;
            }
        }

        [HttpPost]
        [ActionName("Update")]
        public HttpResponseMessage UpdateUser([FromUri]int id,[FromBody]User user)//{"ID":1,"UserName":"Hello","Platform":1,"Status":"User"}
        {
            return Request.CreateResponse(HttpStatusCode.OK, user);
        }

        //[HttpPost]
        //[ActionName("Update")]
        //public HttpResponseMessage UpdateUser(JObject jo)//{"ID":1,"UserName":"Hello","Platform":1,"Status":"User"}
        //{
        //    User user = new User();
        //    user.ID = 1;
        //    user.Platform = Platform.Youtube;
        //    user.Status = "User";
        //    user.UserName = "Hello";

        //    return Request.CreateResponse(HttpStatusCode.OK, user);
        //}

        //[HttpPost]
        //[ActionName("Update")]
        //public void UpdateUser(JObject jo)//不返回值
        //{
           
        //}

        //[HttpPost]
        //[ActionName("Update")]
        //public bool UpdateUser(JObject jo)//返回:true
        //{
        //    return true;
        //}


    }
}