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. ex6的选择器

    前面的话 尽管DOM作为API已经非常完善了,但是为了实现更多的功能,DOM仍然进行了扩展,其中一个重要的扩展就是对选择器API的扩展.人们对jQuery的称赞,很多是由于jQuery方便的元素选择器 ...

  2. Google在三大系统上停止对Chrome Apps的支持

    近年来凭借着低廉的价格和易于管理和追踪的特性,Chrome OS设备逐渐获得了市场的肯定.只是相比较Windows和macOS桌面系统来说,Chrome OS在应用方面依然存在劣势,为此三年前Goog ...

  3. Java工程为什么要加一个biz层

    biz是Business的缩写,实际上就是控制层(业务逻辑层).解释:控制层的主要作用就是协调model层和view层直接的调用和转换.能够有效的避免请求直接进行数据库内容调用,而忽略了逻辑处理的部分 ...

  4. Units Problem: How to read text size as custom attr from xml and set it to TextView in java code

    Here is this topic’s background: I defined a custom View which extends FrameLayout and contains a Te ...

  5. postgresql数据库实用操作

    查模型的列名: select column_name from information_schema.columns  where table_name= 'your_table'; 应用: 1. 给 ...

  6. 【GoLang】函数作为 类型 和 值

    代码示例 package test import ( "fmt" "testing" ) type testInt func(int) bool func is ...

  7. 一个很详细的web.xml讲解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "- ...

  8. Install OE and BitBake

    LeapFrog Explorers: Install OE and BitBake - eLinux.org     http://elinux.org/LeapFrog_Explorers:_In ...

  9. 【Unity3D】Invoke,InvokeRepeating ,Coroutine 延迟调用,周期性调用

    Invoke和InvokeRepeating方法,可以实现延迟调用,和周期调用 第一个是执行一次,第二个是重复执行 void Invoke(string methodName, float time) ...

  10. linux shell 中 printf 与 echo的区别

         echo echo是非常常用的shell命令.参数如下: -e:打开反斜杠字符backslash-escaped的解析,即对/n,/t等字符进行解析,而不视之为两个字符 -E:关闭反斜杠字符 ...