(1)第一个ASP.NET Web API

本文介绍如何使用ASP.NET Web API创建一个简单的RESTful服务。包括安装必要的软件包、配置Global.asax文件、定义WebApiConfig类以设置路由、创建模型类及控制器,并通过Postman进行测试。
196558-20161226101000101-321062959.png
 

Install-Package Microsoft.AspNet.WebApi

196558-20161226101000679-1412484766.jpg




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));
        }
    } 


测试结果
196558-20161226101001398-1204598298.png
 
用 postman 测试
196558-20161226101002554-249061923.png
 







转载于:https://www.cnblogs.com/tangge/p/5960878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值