博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Web API 简单示例
阅读量:4696 次
发布时间:2019-06-09

本文共 3054 字,大约阅读时间需要 10 分钟。

原文:

一、RESTfulWeb API

   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 

   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
GetAllPlayers() { return players; } public IHttpActionResult GetPlayer(int id) { var player = players.FirstOrDefault
(p => p.Id == id); if (player == null) { return NotFound(); } return Ok(player); } }}

 

  添加Html - Index.html页面

 

  编辑代码如下

    NBA App    

All Players

    Search by ID

     

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

     

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

     

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

     

     

    代码下载

    posted on
    2015-07-03 15:07 阅读(
    ...) 评论(
    ...)

    转载于:https://www.cnblogs.com/lonelyxmas/p/4618704.html

    你可能感兴趣的文章
    C# 两个datatable中的数据快速比较返回交集或差集
    查看>>
    关于oracle样例数据库emp、dept、salgrade的mysql脚本复杂查询分析
    查看>>
    adb shell am 的用法
    查看>>
    iOS10 UI教程视图和子视图的可见性
    查看>>
    FindChildControl与FindComponent
    查看>>
    中国城市json
    查看>>
    android下载手动下载Android SDK
    查看>>
    C++学习:任意合法状态下汉诺塔的移动(原创)
    查看>>
    leetcode133 - Clone Graph - medium
    查看>>
    一点小基础
    查看>>
    PHP 自动加载类 __autoload() 方法
    查看>>
    JDK中的Timer和TimerTask详解(zhuan)
    查看>>
    【python练习】ATM&购物商城程序
    查看>>
    nginx 日志问题(\x22)
    查看>>
    装饰器、迭代器、生成器
    查看>>
    类对象作为类成员
    查看>>
    面向对象和面向过程的区别及优劣对比详解
    查看>>
    const与指针
    查看>>
    thsi指针的一些用法及作用
    查看>>
    c++友元
    查看>>