zoukankan      html  css  js  c++  java
  • (1)第一个ASP.NET Web API

     

    Install-Package Microsoft.AspNet.WebApi




    Global.asax
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                //配置API路由
                GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            } 



    路由配置

    App_Start/WebApiConfig.cs

        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services

                // Web API routes
                config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }
        } 




    模型(Model)

    Storages.cs

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

    namespace TmplWebApiDemo.Models
    {
        public class Storages
        {
            public static IEnumerable<Student> Students { get; set; }

            public static IEnumerable<Teacher> Teachers { get; set; }

            static Storages()
            {
                Students = new List<Student>
                  {
                    new Student{Id =1,Name="张三",Age =11,Gender=false},
                    new Student{Id =2,Name="李四",Age =21,Gender=true},
                    new Student{Id =3,Name="王五",Age =22,Gender=false},
                    new Student{Id =4,Name="赵飞燕",Age =25,Gender=true},
                    new Student{Id =5,Name="王刚",Age =31,Gender=true}
                  };
                Teachers = new List<Teacher>
                  {
                    new Teacher{Id =1,Name="老师1",Age =11,Gender=false},
                    new Teacher{Id =2,Name="老师2",Age =21,Gender=true},
                    new Teacher{Id =3,Name="老师3",Age =22,Gender=false},
                    new Teacher{Id =4,Name="老师4",Age =25,Gender=true}
                  };
            }
        }

        public class Person
        {
            public int Id { get; set; }

            public string Name { get; set; }

            public int Age { get; set; }

            public bool Gender { get; set; }
        }

        public class Student : Person
        {

        }

        public class Teacher : Person
        {

        }
    } 




    控制器(Controller)

    StudentsController.cs
        /// <summary>
        /// 学生资源集合
        /// </summary>
        public class StudentsController : ApiController
        {
            //c r u d
            /// <summary>
            /// GET / Students/
            /// </summary>
            public IEnumerable<Student> Get()
            {
                return Storages.Students;
            }


            /// <summary>
            /// GET / students/zhangsan return entity
            /// </summary>
            /// <returns></returns>
            public Student Get(string name)
            {
                return Storages.Students.FirstOrDefault(s => s.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
            }
        } 


    测试结果
     
    用 postman 测试
     







  • 相关阅读:
    linux远程桌面连接 VNC Server
    linux内核 mtd分区
    STC15控制数码管 38译码器
    DS12C887实时时钟
    printf打印字节调试
    LED 控制卡 单元板 接口引脚定义
    linux守护进程start-stop-daemon启动服务
    相机速率计算
    CodeWarrior IDE烧写介绍
    让 Web 站点崩溃最常见的七大原因
  • 原文地址:https://www.cnblogs.com/tangge/p/5960878.html
Copyright © 2011-2022 走看看