首页 技术 正文
技术 2022年11月15日
0 收藏 723 点赞 4,507 浏览 2019 个字

 

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 测试 

null

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848