WebApi项目创建CURD
2019獨角獸企業重金招聘Python工程師標準>>>
WebApi
1.創建實體類
public class Contact
? ? {
? ? ? ? public string Id { get; set; }
? ? ? ? public string Name { get; set; }
? ? ? ? public string Phone { get; set; }
? ? ? ? public string Email { get; set; }
? ? ? ? public DateTime LastModified { get; set; }
? ? }
2.操作實體類接口
public interface IContactRepository
? ? {
? ? ? ? IEnumerable<Contact> GetAllContacts();//獲取全部數據
? ? ? ? Contact GetContact(string id);//根據id獲取數據
? ? ? ? List<Contact> AddContact(Contact item);//添加數據
? ? ? ? bool RemoveContact(string id);//刪除數據
? ? ? ? List<Contact> UpdateContact(Contact item); ??//更新數據
? ? }
3.創建BLL并實現IContactRepository接口
public class ContactBLL : IContactRepository
? ? {
? ? ? ? public IEnumerable<Contact> GetAllContacts()
? ? ? ? {
? ? ? ? ? ? return ContactDAL.GetAllContactsDAL();
? ? ? ? }
? ? ? ? public Models.Contact GetContact(string id)
? ? ? ? {
? ? ? ? ? ? return ContactDAL.GetContactDAL(id);
? ? ? ? }
? ? ? ? public List<Contact> AddContact(Contact item)
? ? ? ? {
? ? ? ? ? ? return ContactDAL.AddContactDAL(item);
? ? ? ? }
? ? ? ? public bool RemoveContact(string id)
? ? ? ? {
? ? ? ? ? ? return ContactDAL.RemoveContactDAL(id);
? ? ? ? }
? ? ? ? public List<Contact> UpdateContact(Contact item)
? ? ? ? {
? ? ? ? ? ? return ContactDAL.UpdateContactDAL(item.Id,item);
? ? ? ? }
? ? }
4.創建DAL操作數據,因為沒有寫數據庫,所以寫了一個集合模擬數據庫
public class ContactDAL
? ? {
? ? ? ? static List<Contact> list = new List<Contact>() {?
? ? ? ? ? ? new Contact(){Id = "1",Name = "小李",Phone = "13800000",Email = "222@qq.com",LastModified = DateTime.Now},
? ? ? ? ? ? new Contact(){Id = "2",Name = "小王",Phone = "13500000",Email = "333@qq.com",LastModified = DateTime.Now}
? ? ? ? };
? ? ? ? public static IEnumerable<Contact> GetAllContactsDAL()
? ? ? ? {
? ? ? ? ? ? var a = list.Where(u => 1 == 1);
? ? ? ? ? ? return a;
? ? ? ? }
? ? ? ? public static Contact GetContactDAL(string id)
? ? ? ? {
? ? ? ? ? ? var contact = list.Where(u => u.Id == id).FirstOrDefault<Contact>();//使用lamdba
? ? ? ? ? ? return contact;
? ? ? ? }
? ? ? ? public static List<Contact> AddContactDAL(Contact item)
? ? ? ? {
? ? ? ? ? ? list.Add(item);
? ? ? ? ? ? return list;
? ? ? ? }
? ? ? ? public static bool RemoveContactDAL(string id)
? ? ? ? {
? ? ? ? ? ? var a = from u in list where u.Id == id select u;//使用linq
? ? ? ? ? ? bool b = list.Remove(a.FirstOrDefault<Contact>());
? ? ? ? ? ? return b;
? ? ? ? }
? ? ? ? public static List<Contact> UpdateContactDAL(string id,Contact item)
? ? ? ? {
? ? ? ? ? ? //var a = list.Where(u => u.Id == id).FirstOrDefault<Contact>();
? ? ? ? ? ? var a = from u in list where u.Id == id select u;
? ? ? ? ? ? Contact contact = a.FirstOrDefault<Contact>();
? ? ? ? ? ? contact.Name = item.Name;
? ? ? ? ? ? return list;
? ? ? ? }
? ? }
5.創建api可以讀寫的控制器
????[AllowCross]//webapi允許跨域訪問
? ? [RoutePrefix("api/contact")]//配置路由
? ? public class ContactController : ApiController
? ? {
? ? ? ? private static readonly IContactRepository _contacts = new ContactBLL();
? ? ? ? [HttpGet]
? ? ? ? public string GetContact()
? ? ? ? {
? ? ? ? ? ? return JsonConvert.SerializeObject(_contacts.GetAllContacts());
? ? ? ? ? ??
? ? ? ? }
? ? ? ? [HttpGet]
? ? ? ? public Contact GetContact(string id)
? ? ? ? {
? ? ? ? ? ? return _contacts.GetContact(id);
? ? ? ? ? ??
? ? ? ? }
? ? ? ? [HttpPost]
? ? ? ? public List<Contact> AddContact([FromBody]Contact value)
? ? ? ? {
? ? ? ? ? ? return _contacts.AddContact(value);
? ? ? ? }
? ? ? ? [HttpPost]
? ? ? ? public List<Contact> UpdateContact([FromBody]Contact value)
? ? ? ? {
? ? ? ? ? ? return _contacts.UpdateContact(value);
? ? ? ? }
? ? ? ? [HttpGet]
? ? ? ? public bool RemoveContact(string id)
? ? ? ? {
? ? ? ? ? ? return _contacts.RemoveContact(id);
? ? ? ? }
? ? ? ??
? ? }
6.路由配置(App_Start中的RouteConfig.cs)
修改:url: "{controller}/{action}/{id}",
7.配置跨域訪問(App_Start中的WebApiConfig.cs)
添加改類
public class AllowCrossAttribute : System.Web.Http.Filters.ActionFilterAttribute
? ? {
? ? ? ? private const string Origin = "Origin";
? ? ? ? /// <summary>
? ? ? ? /// Access-Control-Allow-Origin是HTML5中定義的一種服務器端返回Response header,用來解決資源(比如字體)的跨域權限問題。
? ? ? ? /// </summary>
? ? ? ? private const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
? ? ? ? /// <summary>
? ? ? ? /// ?originHeaderdefault的值可以使 URL 或 *,如果是 URL 則只會允許來自該 URL 的請求,* 則允許任何域的請求
? ? ? ? /// </summary>
? ? ? ? private const string originHeaderdefault = "*";
? ? ? ? /// <summary>
? ? ? ? /// 該方法允許api支持跨域調用
? ? ? ? /// </summary>
? ? ? ? /// <param name="actionExecutedContext"> 初始化 System.Web.Http.Filters.HttpActionExecutedContext 類的新實例。</param>
? ? ? ? public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
? ? ? ? {
? ? ? ? ? ? actionExecutedContext.Response.Headers.Add(AccessControlAllowOrigin, originHeaderdefault);
? ? ? ? }
? ? }
Web端調用
@{
? ? ViewBag.Title = "請求web api";
? ? Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>請求web api</h2>
<div>
? ? <input type="button" value="獲取全部數據" οnclick="GetContactAll()"/>
? ? <input type="button" value="根據id獲取數據" οnclick="GetContactToId()" />
? ? <input type="button" value="添加數據" οnclick="AddContact()" />
? ? <input type="button" value="更新數據" οnclick="UpdateContact()" />
? ? <input type="button" value="刪除數據" οnclick="RemoveContact()" />
</div>
<script>
? ? var path = "http://localhost:32166/api/Contact/";
? ? function GetContactAll() {
? ? ? ? $.get(path + "GetContact", "", function (data) {
? ? ? ? ? ? console.info(data);
? ? ? ? },"json")
? ? }
? ? function GetContactToId() {
? ? ? ? $.get(path + "GetContact", {id:"1"}, function (data) {
? ? ? ? ? ? console.info(data);
? ? ? ? },"json")
? ? }
? ? function AddContact() {
? ? ? ??
? ? ? ? var contact1 ={
? ? ? ? ? ? Id:"3",
? ? ? ? ? ? Name: "小樓",
? ? ? ? ? ? Phone: "123456",
? ? ? ? ? ? Email: "123@qq.com"
? ? ? ? }
? ? ? ? var contact2 = {
? ? ? ? ? ? Id: "4",
? ? ? ? ? ? Name: "小樓",
? ? ? ? ? ? Phone: "123456",
? ? ? ? ? ? Email: "123@qq.com"
? ? ? ? }
????????//兩種方式
? ? ? ? $.post(path + "AddContact", contact1, function (data) {
? ? ? ? ? ? console.info(data);
? ? ? ? }, "json");//第一種
? ? ? ? $.ajax({
? ? ? ? ? ? type: "post",
? ? ? ? ? ? url: path + "AddContact",
? ? ? ? ? ? dataType: "json",
? ? ? ? ? ? data: contact2,
? ? ? ? ? ? async: true,
? ? ? ? ? ? success: function (data) {
? ? ? ? ? ? ? ? console.info(data);
? ? ? ? ? ? }
? ? ? ? })//第二種
? ? }
? ? function UpdateContact() {
? ? ? ? var contact= {
? ? ? ? ? ? Id:"1",
? ? ? ? ? ? Name: "小樓",
? ? ? ? ? ? Phone: "123456",
? ? ? ? ? ? Email: "123@qq.com"
? ? ? ? }
? ? ? ??
? ? ? ? $.post(path + "UpdateContact", contact, function (data) {
? ? ? ??
? ? ? ? ? ? console.info(data);
? ? ? ? },'json')
? ? }
? ? function RemoveContact() {
? ? ? ? $.get(path + "RemoveContact", { id: "1" }, function (data) {
? ? ? ? ? ? console.info(data);
? ? ? ? },'json');
? ? }
</script>
?
?
轉載于:https://my.oschina.net/u/3141521/blog/1825774
總結
以上是生活随笔為你收集整理的WebApi项目创建CURD的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用VideoView做个实用的视频播放
- 下一篇: flask学习笔记之flask-migr