asp.net之使用web API
来源:互联网 发布:淘宝店家可能亏本吗 编辑:程序博客网 时间:2024/05/29 19:22
http Get 方法请求获取数据,整个web API 的请求处理基于MVC框架
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Http;using System.Web.Http;using WebAPI.Models;namespace WebAPI.Controllers{ public class UsersController : ApiController { /// <summary> /// User Data List /// </summary> private readonly List<Users> _userList = new List<Users> { new Users {UserID = 1, UserName = "Superman", UserEmail = "Superman@cnblogs.com"}, new Users {UserID = 2, UserName = "Spiderman", UserEmail = "Spiderman@cnblogs.com"}, new Users {UserID = 3, UserName = "Batman", UserEmail = "Batman@cnblogs.com"} }; // GET api/Users public IEnumerable<Users> Get() { return _userList; } // GET api/Users/5 public Users GetUserByID(int id) { var user = _userList.FirstOrDefault(users => users.UserID == id); if (user == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return user; } //GET api/Users/?username=xx public IEnumerable<Users> GetUserByName(string userName) { return _userList.Where(p => string.Equals(p.UserName, userName, StringComparison.OrdinalIgnoreCase)); } }}构造 user list ,实现三个方法。
POST数据
实现一个User添加的功能,接受的类型为User实体,而我们POST的数据为对应的JSON数据。
//POST api/Users/Users Entity Jsonpublic Users Add([FromBody]Users users){ if (users == null) { throw new HttpRequestException(); } _userList.Add(users); return users;}
var requestJson = JsonConvert.SerializeObject(new { startId = 1, itemcount = 3 });HttpContent httpContent = new StringContent(requestJson);httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");var httpClient = new HttpClient();var responseJson = httpClient.PostAsync("http://localhost:9000/api/demo/sitelist", httpContent) .Result.Content.ReadAsStringAsync().Result;升级版为:
var responseJson = new HttpClient().PostAsJsonAsync("http://test.cnblogs.cc/api/demo/sitelist",new { startId = 1, itemcount = 3 }).Result.Content.ReadAsStringAsync().Result;
0 0
- asp.net之使用web API
- ASP.NET Web API 之 WebApiConfig
- ASP.NET Web API 之 ValuesController
- ASP.NET Web API之FluentValidation验证
- ASP.NET Web API使用示例
- ASP.NET Web API使用示例
- asp.net web api
- ASP.NET Web API
- asp.net web api
- asp.net web api
- asp.net web api
- 使用ASP.NET Web API构建RESTful API
- ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)
- ASP.NET Web API(二):安全验证之使用HTTP基本认证
- ASP.NET Web API:安全验证之使用HTTP基本认证
- ASP.NET Web API:安全验证之使用摘要认证(digest authentication)
- Asp.Net MVC 之 Autofac 初步使用3 集成web api
- Asp.Net MVC 之 Autofac 初步使用3 集成web api
- (OK) http://www.android-x86.org - cm-13.0-x86 - download
- (OK) http://www.android-x86.org - cm-13.0-x86 - compile
- python修改文本颜色
- JAVA开发7--java发布webservice简单示例
- [kuangbin带你飞]专题六 最小生成树 C POJ 2031
- asp.net之使用web API
- Hidden terminal and Exposed terminal
- Undefined index: user in E:\wamp\wamp\www\liuyanbanban\login.php on line 4
- 使用 SSHFS 挂载远程的 Linux 文件系统及目录
- 正则应用方法
- unity 3d中 NGUI和UGUI分别是什么?相对于外部UI插件我们要使用哪个呢?
- 如何正确结束Java线程
- 个体行为对沟通的影响
- 20种匹配正则