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来管理 ...
随机推荐
- Bzoj1486/洛谷P3199 最小圈(0/1分数规划+spfa)/(动态规划+结论)
题面 Bzoj 洛谷 题解(0/1分数规划+spfa) 考虑\(0/1\)分数规划,设当前枚举到的答案为\(ans\) 则我们要使(其中\(\forall b_i=1\)) \[ \frac{\sum ...
- 设计模式-外观模式(Facade Pattern)
本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 外观模式简介 外观模式的作用用一句话说就是简化接口,举个例子楼主每次编程的时候都要点开IDE.点 ...
- 【BZOJ 4025】 (CDQ?还是整体二分?+并查集及它的恢复操作)
4025: 二分图 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简单的问题神犇当然会做了,于是他想考 ...
- oracle latch工作原理
latch是一种轻量级用于保护oracle共享内存结构,用户并发操作一致性的串行化锁定机制,如SGA中,各种数据被反复从磁盘读取到内存,又被重新写回到磁盘上,如果有并发用户做相同的事情,oracle必 ...
- SQL查询中关键词的执行顺序
写在前面:最近的工作主要是写SQL脚本,在编写过程中对SQL的执行和解析过程特别混乱不清,造成了想优化却无从下手.为此专门在网上找博文学习,并做了如下总结. 1.查询中常用到的关键词有: SELECT ...
- JZYZOJ1355 [usaco2007]奶牛赛跑 矩阵乘法 离散化
http://172.20.6.3/Problem_Show.asp?id=1355 写的时候本来想离散化,“1000^2的数组放一两个到函数里而已嘛,指定承受得住”,然后没离散化,然后就爆栈了, ...
- 2017 icpc 南宁网络赛
2000年台湾大专题...英语阅读输入输出专场..我只能说很强势.. M. Frequent Subsets Problem The frequent subset problem is define ...
- 【朱-刘算法】【最小树形图】hdu6141 I am your Father!
题意:给你一张带权有向图,让你求最大树形图.并在此前提下令n号结点父亲的编号最小. 比赛的时候套了个二分,TLE了. 实际上可以给每个边的权值乘1000,对于n号结点的父边,加上(999-父结点编号) ...
- 【最小生成树】【kruscal】hdu4786 Fibonacci Tree
假设这张图能够形成具有k条白边的生成树, 则易证k一定形成一个连续的区间[a,b],中间一定不会断开.要是断开……tm怎么可能. 所以求出a,b就好啦,人家都给你把白边赋成1了,直接跑一下最小生成树, ...
- 20162318 实验一《Java开发环境的熟悉》实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程:程序设计与数据结构 班级:1623班 姓名:张泰毓 成绩:2分 指导老师:娄老师.王老师 实验日期:2017年3月17日 实验密级:非密级 实验器 ...