• 1.1 .1 WebAPI构建解决方案


         为了让读者先对ASP.NET Web Api 有一个感性的认知,我们手动创建一个 Web API程序

        

        创建一个空的项目,然后添加一下内容:

        Common::空的类库项目。

        Webapi:空的ASP.NET Web 应用,引用Common

        WebHost:空的ASP.NET Web 应用,它实现了针对ASP.NET Web api的Web Host寄宿,引用webapi

        SelHosst:空的控制台应用,它实现了针对ASP.NET Web api的Self Host寄宿,引用webapi

        WebApp:空的ASP.NET Web 应用,重要用于ajax实现调用webapi

        ConsoleApp:空的控制台应用,主要用于模拟客户端代理实现webapi远程调用

    在Common创建Contact和Information

    public class Contact
        {
            /// <summary>
            /// ID
            /// </summary>
            public string ID { get; set; }
            /// <summary>
            /// 姓名
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            /// 联系电话
            /// </summary>
            public string PhoneNo { get; set; }
            /// <summary>
            /// 电子邮箱
            /// </summary>
            public string EmailAddress { get; set; }
            /// <summary>
            /// 联系地址
            /// </summary>
            public string Address { get; set; }
            /// <summary>
            /// 资料
            /// </summary>
            public List<Information> lisInfo { get; set; }
        }
    
        public class Information
        {
            public string Education { get; set; }
        }

        

      Webapi引入程序集System.Web.Http,并创建ContactsController

      我们定义了Get,Post,Put,Delete这4个Action方法

        public class ContactsController : ApiController
        {
            static List<Contact> contacts;
    
            static ContactsController()
            {
                contacts = new List<Contact>() {
                    new Contact { ID = "001", Name = "张三", PhoneNo = "15811100056", EmailAddress = "zhangsanemail@163.com", Address = "北京市", lisInfo = new List<Information>() { new Information { Education = "小学" }, new Information { Education = "中学" } } },
                    new Contact { ID = "002", Name = "李四", PhoneNo = "0474-577466", EmailAddress = "lisiemail@163.com", Address = "内蒙古", lisInfo = new List<Information>() { new Information { Education = "高中" }, new Information { Education = "大学" } } }
                };
            }
    
            public IEnumerable<Contact> Get(string id = null)
            {
                return from contact in contacts
                       where contact.ID == id || string.IsNullOrEmpty(id)
                       select contact;
            }
    
            public void Post(Contact contact)
            {
                contact.ID = "003";
                contacts.Add(contact);
            }
    
    
            public void Put(Contact contact)
            {
                contacts.Remove(contacts.First(C => C.ID == contact.ID));
                contacts.Add(contact);
            }
    
    
            public void Delete(string id)
            {
                contacts.Remove(contacts.First(C => C.ID == id));
            }
        }
  • 相关阅读:
    编程基础之Java中继承和多态的理解
    2020年程序员是否过剩?
    如何自学编程,零基础适合学习Java或者Web前端吗,非科班的能学java吗?
    转行IT做后端开发,学python还是java?
    java和大数据哪个更有前途啊?零基础适合学哪个课程?
    随笔(二十)
    随笔(十九)
    随笔(十八)
    OSPF练习
    OSPF综合实验(二)
  • 原文地址:https://www.cnblogs.com/fenger-VIP/p/6677725.html
Copyright © 2020-2023  润新知