一、RESTfulWeb API

  Representational State Transfer (REST) is a software architecture style consisting of guidelines and best practices for creating scalable web services. REST is a coordinated set of constraints applied to the design of components in a distributed hypermdedia system that can lead to a more performant and maintainable architecture.  -- wikipedia 

  ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

  原来RESTful是一种软件架构风格(REST是一种设计风格,而不是一种标准),而ASP.NET Web API是其在.NET平台的一种标准/实现。目前在三种主流的Web Services实现方案中,因为REST模式与复杂的SOAPXML -PRC相比更加简洁,越来越多的web服务开始采用REST风格设计和实现。

  ASP.NET整体框架结构如下图。可以看出,Web API支持JSONXML,面向的是多种客户终端,包括多浏览器和各种移动设备。

二、简单示例

  新建ASP.NET Web Application,命名NBAApp

  

  选择Empty模板,下面选择Web API,更改AuthenticationNo Authentication

  新建一个Model - Player 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;namespace NBAApp.Models
{public class Player{public int Id { get; set; }public int No { get; set; }public string Name { get; set; }public string Position { get; set; }public string Team { get; set; }}
}

  新建Controller - PlayersController,模板选择Web API 2 Controller - Empty

  

  编辑代码如下

using NBAApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;namespace NBAApp.Controllers
{public class PlayersController : ApiController{Player[] players = new Player[] { new Player { Id = 1, No = 3, Name = "Chris Paul", Position = "Point Guard", Team = "Los Angeles Clippers" },new Player { Id = 2, No = 3, Name = "Dwyane Wade", Position = "Shooting Guard", Team = "Miami Heat" },new Player { Id = 3, No = 23, Name = "LeBron James", Position = "Forward", Team = "Cleveland Cavaliers" },new Player { Id = 4, No = 21, Name = "Tim Duncan", Position = "Power forward", Team = "San Antonio Spurs" },new Player { Id = 5, No = 33, Name = "Marc Gasol", Position = "Center", Team = "Memphis Grizzlies" }};public IEnumerable<Player> GetAllPlayers(){return players;}public IHttpActionResult GetPlayer(int id){var player = players.FirstOrDefault<Player>(p => p.Id == id);if (player == null){return NotFound();}return Ok(player);}}
}

  添加Html - Index.html页面

  编辑代码如下

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>NBA App</title>
</head>
<body><div><h2>All Players</h2><ul id="players" /></div><div><h2>Search by ID</h2><input type="text" id="prodId" size="5" /><input type="button" value="Search" onclick="find();" /><p id="player" /></div><script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script><script>var uri = 'api/players';$(document).ready(function () {// Send an AJAX request
            $.getJSON(uri).done(function (data) {// On success, 'data' contains a list of players.
                    $.each(data, function (key, item) {// Add a list item for the player.
                        $('<li>', { text: formatItem(item) }).appendTo($('#players'));});});});function formatItem(item) {return item.Id + ": " + item.Name + "(" + item.No + ')' + " - " + item.Team + "(" + item.Position + ")";}function find() {var id = $('#prodId').val();$.getJSON(uri + '/' + id).done(function (data) {$('#player').text(formatItem(data));}).fail(function (jqXHR, textStatus, err) {$('#player').text('Error: ' + err);});}</script>
</body>
</html>

  执行效果如下(Chrome浏览器)

  F12调出Developer Tools,点击红点Recording Network Log,刷新页面,结果如下

  点击进去,并选择Response标签,可以清楚地看到传输交换的是JSON格式的字符

代码下载

NBAApp

转载于:https://www.cnblogs.com/panchunting/p/WebAPI_Demo.html

Web API 简单示例相关推荐

  1. ASP.NET Web API 开篇示例介绍

    ASP.NET Web API 开篇示例介绍 ASP.NET Web API 对于我这个初学者来说ASP.NET Web API这个框架很陌生又熟悉着. 陌生的是ASP.NET Web API是一个全 ...

  2. 语义web一些简单示例

    语义web一些简单示例 示例1 (xml部分-用xml描述书籍信息) 题目:写出三本书,每本书有:标题,作者,出版社,出版日期,,,,,:写出对应的XML文件:根据你写的XML文件,写出对应的DTD文 ...

  3. ASP.NET Web API简单学习

    Web API 简介 Web API可以返回 json.xml类型的数据,对于数据的增删改查,提供了对应的资源操作,按照要求的类型进行处理,主要包括:Get(查),Post(增),Put(改),Del ...

  4. ASP.net Web API综合示例

    目录 概述 功能介绍 程序结构 服务器端介绍 客户端介绍 "契约" Web API设计规则 并行写入冲突与时间戳 身份验证详解 Web API验证规则 客户端MVVM简介 Web. ...

  5. java调用百度地图api简单示例--获取国内任意两地之间距离

    老师让我们从百度地图的api上获取数据源最为两地运输距离,结果百度地图api的开发文档居然连个示例都没有...于是上网找了半天,都是一百多行的源码,我就想用个api,你给我这玩意???终于最后还是找到 ...

  6. php post调用api,PHP(CURL)POST数据调用API简单示例

    /** *@一个完整的POST调用API的过程 百度知道 *@author: bo.xiao */ $url = 'http://zhidao.chanjet.com/restserver/zhida ...

  7. Linux下MySQL C API简单示例

    2019独角兽企业重金招聘Python工程师标准>>> 1. 创建数据库 drop database testdb; commit;create database testdb; c ...

  8. java web service简单示例

    http://www.iteye.com/topic/1135747 转载于:https://www.cnblogs.com/fycct/p/5669420.html

  9. 使用Http-Repl工具测试ASP.NET Core 2.2中的Web Api项目

    今天,Visual Studio中没有内置工具来测试WEB API.使用浏览器,只能测试http GET请求.您需要使用Postman,SoapUI,Fiddler或Swagger等第三方工具来执行W ...

最新文章

  1. 听完411头猪的哼哼,他们找到了理解“猪语”的算法 | Scientific Reports
  2. 如何在Python中获取字符串的子字符串?
  3. [bootstrapValidator] - bootstrap的验证工具
  4. 工业4.0的小小思考
  5. 华为5G英国首秀,BBC主持人震惊了!到底网速有多快?
  6. ubuntu 自动加载ko_linux驱动模块开机自动加载,以及应用程序开机自启动
  7. CentOS7安装Python3.4 ,让Python2和3共存
  8. Linux学习(六):命令与文件的查阅,Root用户和个人用户使用which命令的差别...
  9. js 页面载入时的执行顺序
  10. android学习笔记---64_ListView数据异步加载与AsyncTask
  11. 最小生成树--通公路问题
  12. 刚刚,自动驾驶路测国家规范出台:无人车即将开上更多实际道路
  13. vue学习笔记-8-循环结构
  14. 001:InVEST学习——产水/水源涵养
  15. 机器视觉知识汇总(持续更新)
  16. 「2021年」国内主流短信验证码平台综合评测
  17. 计算机高一教案,《计算机系统的基本组成》高一信息技术课教案
  18. glide 设置宽高_glide如何设置图片大小
  19. HTML邮件模板编写规则,编写邮件HTML模板
  20. 《2022女程序员人群洞察报告》

热门文章

  1. docker 安装zookeeper集群
  2. MySQL全面优化,速度飞起来!
  3. Java 集合框架综述
  4. Spring核心技术原理-(1)-通过Web开发演进过程了解一下为什么要有Spring?
  5. 初识Kubernetes
  6. 杨辉三角python语言程序思路_python杨辉三角方法的实例介绍
  7. 『机房工程』弱电必备技能培训PPT/值得您分享
  8. 成功解决pywintypes.com_error: (-2147352573, ‘找不到成员。‘, None, None)
  9. DayDayUp:大学英语六级考试历年真题答案规律分析、应试回答精讲、及六级改革历史之详细攻略
  10. 成功解决schedule.ScheduleValueError: Invalid time format