(1)第一个ASP.NET Web API

。
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 }
);
}
}
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
{
}
}
/// <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));
}
}


(1)第一个ASP.NET Web API的更多相关文章
- 【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 ...
- 一个ASP.NET Web API 2.0应用
在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用 由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.N ...
- 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用
由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...
- 第一个ASP.NET Web API (C#)程序
本文翻自http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api 绝对手工制作,如有雷同,实属巧合. 转载请注明. ...
- 如何创建一个Asp .Net Web Api项目
1.点击文件=>新建=>项目 2.创建一个Asp .NET Web项目 3.选择Empty,然后选中下面的MVC和Web Api,也可以直接选择Web Api选项,注意将身份验证设置为无身 ...
- ASP.NET Web API 记录请求响应数据到日志的一个方法
原文:http://blog.bossma.cn/dotnet/asp-net-web-api-log-request-response/ ASP.NET Web API 记录请求响应数据到日志的一个 ...
- How ASP.NET Web API 2.0 Works?[持续更新中…]
一.概述 RESTful Web API [Web标准篇]RESTful Web API [设计篇] 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用 二.路由 ...
- ASP.NET Web API中的依赖注入
什么是依赖注入 依赖,就是一个对象需要的另一个对象,比如说,这是我们通常定义的一个用来处理数据访问的存储,让我们用一个例子来解释,首先,定义一个领域模型如下: namespace Pattern.DI ...
- OData services入门----使用ASP.NET Web API描述
http://www.cnblogs.com/muyoushui/archive/2013/01/27/2878844.html ODate 是一种应用层协议,设计它的目的在于提供一组通过HTTP的交 ...
随机推荐
- python 函数的调用 和执行 小知识
1.符号表 执行一个函数会引入一个用于函数的局部变量的新符号表. 更确切地说, 函数中的所有的赋值都是将值存储在局部符号表: 而变量引用首先查找局部符号表, 然后是上层函数的局部符号表, 然后是全局符 ...
- HTML页面关键词随机分布布局
结合underscore-min.js,和D3.js 绘制HTML关键词随机分布 <!DOCTYPE html> <html> <head> <meta ch ...
- 获取客户端IP
function getIP(){ $ip = ""; if (getenv("HTTP_CLIENT_IP") && strcasecmp(g ...
- Eclipse中项目红叉但找不到错误解决方法
首先windows-show view-problems 根据地址查找错误 若提示: Description Resource Path Location TypeJava c ...
- ZJOI day1总结
虽然没人看,虽然滚了大粗,但还是这样勉励一下自己.. 今年大约是进队无望了. before ZJOI 感觉自己时间很充裕,与lyx大爷一起颓颓颓.. day -3 到xj. day -2 听课.感觉洲 ...
- Xcode开发技巧之Code Snippets Library
http://blog.csdn.net/lin1986lin/article/details/21180007 目录(?)[-] 引言 什么是Code Snippets 如何新建Code Snipp ...
- C++中Cstring、wstring 和string互相转换总结
通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操 ...
- Qt ini配置文件读写
#include <QSettings> void MainWindow::saveIni() { //如果ini文件不存在,创建新ini文件,存在赋值 QSettings *setIni ...
- Effective C++ -----条款20:宁以pass-by-reference-to-const替换pass-by-value Prefer pass-by-reference-to-const to pass-by-value
尽量以pass-by-reference-to-const替换pass-by-value.前者通常比较高校,并可避免切割问题(slicing problem). 以上规则并不适用于内置类型,以及STL ...
- js闭包问题
function picLinkInit(parentClassName, imgW, imgH, childClassObjs) { var $match = $(parentClassName); ...