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



(1)第一个ASP.NET Web API的更多相关文章

  1. 【ASP.NET Web API教程】1.1 第一个ASP.NET Web API

    Your First ASP.NET Web API (C#)第一个ASP.NET Web API(C#) By Mike Wasson|January 21, 2012作者:Mike Wasson ...

  2. 一个ASP.NET Web API 2.0应用

    在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用 由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.N ...

  3. 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用

    由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...

  4. 第一个ASP.NET Web API (C#)程序

    本文翻自http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api 绝对手工制作,如有雷同,实属巧合. 转载请注明. ...

  5. 如何创建一个Asp .Net Web Api项目

    1.点击文件=>新建=>项目 2.创建一个Asp .NET Web项目 3.选择Empty,然后选中下面的MVC和Web Api,也可以直接选择Web Api选项,注意将身份验证设置为无身 ...

  6. ASP.NET Web API 记录请求响应数据到日志的一个方法

    原文:http://blog.bossma.cn/dotnet/asp-net-web-api-log-request-response/ ASP.NET Web API 记录请求响应数据到日志的一个 ...

  7. How ASP.NET Web API 2.0 Works?[持续更新中…]

    一.概述 RESTful Web API [Web标准篇]RESTful Web API [设计篇] 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用 二.路由 ...

  8. ASP.NET Web API中的依赖注入

    什么是依赖注入 依赖,就是一个对象需要的另一个对象,比如说,这是我们通常定义的一个用来处理数据访问的存储,让我们用一个例子来解释,首先,定义一个领域模型如下: namespace Pattern.DI ...

  9. OData services入门----使用ASP.NET Web API描述

    http://www.cnblogs.com/muyoushui/archive/2013/01/27/2878844.html ODate 是一种应用层协议,设计它的目的在于提供一组通过HTTP的交 ...

随机推荐

  1. 架设 OpenLDAP服务器

    OpenLDAP是一个开放源代码的软件,可以免费获取使用,其主页地址是:http://www.openldap.org/.在RHEL 6上安装OpenLDAP还需要libtool-ltdl-2.2.6 ...

  2. 关于man和help的区别

    help 是内部命令的帮助,比如cdman 是外部命令的帮助,比如ls

  3. Android应用反破解的思路

    一个Android应用要被破解,要经历:反编译->分析代码->重新编译打包的过程,反破解的思路也是从在这三个步骤上做文章: 1, 寻找反编译工具的缺陷,通过阅读其源码或者对其进行压力测试找 ...

  4. Git-it字典翻译

    Git-it字典翻译 下面的内容翻译自git-it/dictionary 水平有限,翻译欠佳. Git准备工作 创建一个新的文件夹(目录) $ mkdir <目录名称> 切换到这个目录 ( ...

  5. PHP判断用户操作系统(Android,ipad,iphone,windows)

    这段脚本可以运用在:针对不同的操作系统,把用户引导向相应的网站或做相应的处理. <?php // PHP 判断客户端平台(PC.安卓.iPhone.平板) // strpos() 函数返回字符串 ...

  6. 使用 MongoDB 的_id 查询

    MongoDB 默认在插入数据时,生成一个主键_id,那么怎么使用_id来查询数据? 查询全部 > db.foo.find(){ "_id" : ObjectId(" ...

  7. 【Networking】go get 失败,代理配置

    推荐VPN: https://vpnso.com/   如果还是有问题,比如: 重新编译Git,使用openssl替换gnutls,方法如下: http://askubuntu.com/questio ...

  8. 【Java】Java 深入探讨 单例模式的实现

    在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单例设计模式详细的探讨一下.   所谓单例模式,简单来说,就是在整个应用中保证只有一个类的实例存在 ...

  9. Unity3d 解析文本执行已注册的自定函数

    最近有个需求是想让程序解析策划编辑一个文本生成一段CG,内容使用大致是这样 cgSetCameraEx(118.6324,30.71189,75.55666,45,-45,0,0) cgCloneMy ...

  10. c# 获取系统版本,获取net framework 版本(Environment 类)

    1.获取当前操作系统版本信息 使用Environment.OSVersion 属性 获取包含当前平台标识符和版本号的 OperatingSystem 对象. 命名空间:  System程序集:  ms ...