ASP.NET MVC & Web API Brief Introduction
Pure Web Service(ASMX):
Starting back in 2002 with the original release of .NET, a developer could fairly easily create an ASP.NET
ASMX-based XML web service that allowed other .NET and non-.NET clients to call it.
Those web services implemented various versions of SOAP, but were only available for
use over HTTP.
.NET Remoting:
.NET Remoting essentially provides object activation and session context for client-initiated method calls. The caller uses a proxy
object to invoke methods, and the .NET runtime handles serialization and marshaling of data between the client’s proxy
object and the server’s activated service object.
Windows Communication Foundation (WCF):
Towards the end of 2006, Microsoft released .NET 3.0, which included the Windows
Communication Foundation (WCF). WCF not only replaced ASMX web services and
.NET Remoting, but also took a giant step forward in the way of flexibility, configurability,
extensibility, and support for more recent security and other SOAP standards.
For example, with WCF, a developer can write a non-HTTP service that supports
authentication with SAML tokens, and host it in a custom-built Windows service. These
and other capabilities greatly broaden the scenarios under which .NET can be utilized to
build a service-oriented application.
Now:
In addition to simpler protocol and security needs, web pages typically communicate
with other applications and services using text-based messages rather than binary-formatted
messages. As such, a service needs only to support XML or JSON serialization.
Advantages of Using the MVC Framework:
Speaking of REST, building services with ASP.NET MVC and the Web API provides most of
what you need to adhere to the constraints of the REST architecture. This is largely due to
the URL routing feature provided by the MVC Framework. Unlike WCF, where a service is
an address to a physical file (i.e., an address that maps directly to a service class or
.svc file), service addresses with MVC are REST–style routes that map to controller
methods. As such, the paths lend themselves very nicely to REST–style API specifications.
WCF Implementation
http://MyServer/TaskService.svc
[ServiceContract]
public interface ITaskService
{
[OperationContract]
Task GetTask(long taskId);
}
public class TaskService : ITaskService
{
private readonly IRepository _repository;
public TaskService(IRepository repository)
{
_repository = repository;
}
public Task GetTask(long taskId)
{
return _repository.Get<Task>(taskId);
}
}
MVC:
public class TasksController : Controller
{
private readonly IRepository _repository;
public TasksController(IRepository repository)
{
_repository = repository;
}
public ActionResult Get(long taskId)
{
return Json(_repository.Get<Task>(taskId));
}
}
Web API:
public class TasksController : ApiController
{
private readonly IRepository _repository;
public TasksController(IRepository repository)
{
_repository = repository;
}
public Task Get(long taskId)
{
return repository.Get<Task>(taskId);
}
}
One of the biggest changes is the base class used by the new controller,
ApiController. This base class was built specifically for enabling RESTful services, and
you simply return the object (or, objects in a collection) of the data being requested.
Contrast this with the ActionResult shown in the preceding MVC4 example. Further, the
URL itself will be different.
A QUICK OVERVIEW OF REST
Created by Roy Fielding, one of the primary authors of the HTTP specification,
REST is meant to take better advantage of standards and technologies within HTTP
than SOAP does today. For example, rather than creating arbitrary SOAP methods,
developers of REST APIs are encouraged to use only HTTP verbs.
* GET
* POST
* PUT
* DELETE
Web API Brief:
* Convention-based CRUD Actions:
HTTP actions (e.g., GET and POST) are automatically mapped to controller methods
(also known as controller actions) by their names. For example,
on a controller called Products, a GET request such as
/api/products will automatically invoke a method named “Get”
on the controller. Further, the Web API automatically matches
the number of arguments given in the URL to an appropriate
controller method. Therefore, the URL /api/products/32 would
automatically invoke the Get(long id) method. The same magic
also applies to POST, PUT, and DELETE calls.
* Built-in Content Negotiation:
In MVC, controller methods that return JSON or XML have to be hard-coded to specifically return
one of those content types. But with the Web API, the controller
method need only return the raw data value, and this value will be
automatically converted to JSON or XML, per the caller’s request.
The caller simply uses an Accept or Content-Type HTTP header
to specify the desired content type of the returned data, and the
Web API ensures your return value gets formatted appropriately.
Rather than returning an object of type JsonResult, you simply
return your data object (e.g., Product or IEnumerable<Product>).
* Automatic support for OData:
By simply placing the new [Queryable] attribute on a controller method that returns
IQueryable, clients can use the method for OData query
composition.
* Self-hosting:
With the Web API, you no longer need to use IIS to
host HTTP services. Now your REST services can be hosted in a
custom Windows service, console application, or any other type
of host you need.
ASP.NET MVC & Web API Brief Introduction的更多相关文章
- ASP.NET MVC Web API Post FromBody(Web API 如何正确 Post)
问题场景: ASP.NET MVC Web API 定义 Post 方法,HttpClient 使用 JsonConvert.SerializeObject 传参进行调用,比如 Web Api 中定义 ...
- ASP.NET MVC Web API For APP
近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过在浏览器中使用 JavaScr ...
- [译]ABP框架使用AngularJs,ASP.NET MVC,Web API和EntityFramework构建N层架构的SPA应用程序
本文转自:http://www.skcode.cn/archives/281 本文演示ABP框架如何使用AngularJs,ASP.NET MVC,Web API 和EntityFramework构建 ...
- 【转载】ASP.NET MVC Web API 学习笔记---联系人增删改查
本章节简单介绍一下使用ASP.NET MVC Web API 做增删改查.目前很多Http服务还是通过REST或者类似RESP的模型来进行数据操作的.下面我们通过创建一个简单的Web API来管理联系 ...
- Asp.net mvc web api 在项目中的实际应用
Asp.net mvc web api 在项目中的实际应用 前言:以下只是记录本人在项目中的应用,而web api在数据传输方面有多种实现方式,具体可根据实际情况而定! 1:数据传输前的加密,以下用到 ...
- ASP.NET MVC Web API 学习笔记---第一个Web API程序
http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...
- 实战 ASP.NET MVC Web API
实战 ASP.NET MVC Web API Web API 框架基于 ASP.NET MVC 框架开发,是一个面向 Http 协议的通信框架.相对于 WCF 而言,Web API 只面向于 Http ...
- ABP 教程文档 1-1 手把手引进门之 AngularJs, ASP.NET MVC, Web API 和 EntityFramework(官方教程翻译版 版本3.2.5)含学习资料
本文是ABP官方文档翻译版,翻译基于 3.2.5 版本 转载请注明出处:http://www.cnblogs.com/yabu007/ 谢谢 官方文档分四部分 一. 教程文档 二.ABP 框架 三. ...
- ASP.NET MVC Web API 学习笔记---联系人增删改查
本章节简单介绍一下使用ASP.NET MVC Web API 做增删改查. 目前很多Http服务还是通过REST或者类似RESP的模型来进行数据操作的. 下面我们通过创建一个简单的Web API来管理 ...
随机推荐
- 使用mongo shell和客户端连接至MongoDB Atlas
MongoDB Atlas是Mongo官方的一个集群服务,也可以注册并创建一个免费的集群,但DB的大小只有500M,如果数据量不是很大的应用,可以选择该集群方案 需要注意的是,目前我使用的这个集群,服 ...
- ThoughtWorks测试
Fizz-Buzz-Whizz 问题描述: 1. 你首先说出三个不同的特殊数,要求必须是个位数,比如3.5.7. 2. 让所有学生拍成一队,然后按顺序报数. 3. 学生报数时,如果所报数字是第一个特殊 ...
- HDU3466 Proud Merchants [背包]
题目传送门 Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/O ...
- git "Could not read from remote repository.Please make sure you have the correct access rights."解决方案
我们在使用git clone 或其他命令的时候,有时候会遇到这类问题,如图: fatal: Could not read from remote repository.Please make sure ...
- Linux命令之rlogin
rlogin [-8EKLdx] [-e char] [-l username] host rlogin在远程主机host上开始一个终端会话. (1).选项 -8 选项允许进行8位的输入数据传送:否则 ...
- CDH-hive支持insert、update、delete操作
开发要求hive支持update操作,搞张临时表测试下,报错如下: 配置hive-site.xml CDH进入hive配置页,选择高级,找到hive-site.xml的HIve客户端高级配置段代码段, ...
- 「NOI2018」归程
「NOI2018」归程 题目描述 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个 >\(1\) 个节点. \(m\) 条边的无向连通图(节点的编号从 \( ...
- 【欧拉回路】Play On Words(6-16)
[UVA10129]Play On Words 算法入门经典第6章6-16(P169) 题目大意:有一些单词,问能不能将它们串成字符串(只有前缀和后缀相同才能连) 试题分析:很巧妙的一道题,将每个单词 ...
- [AGC026B]rng_10s
题意:一个商店,第一天早上有$A$罐苹果汁,一个人每天上午都会来买$B$罐,每天晚上如果剩余罐数$\leq C$那么会增加$D$罐,问能否无限买 收集一个套路...? 首先如果$B\gt A$或者$B ...
- 【并查集】Gym - 101128B - Black Vienna
有26张牌(A~Z),其中三张被拿走了.其余23张被分发给了两个人.给你m次调查结果,一次调查结果是对其中一个人询问一对牌,他会告诉你他有这对牌的几张(0~2).问你有多少种被拿走的牌的组合. 三重循 ...