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:

http://MyServer/Task/Get/123

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:

http://MyServer/Tasks/123

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.

 

时间: 2024-10-16 07:01:04

ASP.NET MVC & Web API Brief Introduction的相关文章

ASP.NET MVC Web API 学习笔记---联系人增删改查

本章节简单介绍一下使用ASP.NET MVC Web API 做增删改查.目前很多Http服务还是通过REST或者类似RESP的模型来进行数据操作的.下面我们通过创建一个简单的Web API来管理联系人          说明:为了方便数据不使用真正的数据库,而是通过内存数据模拟    1.       Web API中包含的方法 Action HTTP method Relative URI GetAllContact GET /api/contact GetContact GET /api/

ASP.NET MVC Web API 学习笔记----HttpClient简介

  1. HttpClient简单介绍  依稀还记得那个时候用WebClient,HttpWebRequest来发送一个请求,现在ASP.NET MVC4中自带了一个类HttpClient,用于接收HttpResponseMessage和发送HttpRequestMesssage. 问题在于既然WebClient,HttpWebRequest可以完成相应的功能,为什么还要使用HttpClient类,.NET Framework中既然提出了这样一个类肯定是有其特别之处的,这里罗列几个不同之处: (

ASP.NET MVC Web API 学习笔记---第一个Web API程序

1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过在浏览器中使用 JavaScript来创建更丰富的HTML体验.所以我相信Web API会越来越有它的用武之地. 说道Web API很多人都会想到Web服务,但是他们仍然有一定的区别:Web API服务是通过一般的 HTTP公开了,而不是通过更正式的服务合同 (如SOAP)  2. ASP.NET

asp.net mvc web api问题

问题描述 <script>functiontest(){varno={no:"SPH"};$.ajax({type:'POST',url:'/API/B2B/GetNo',data:JSON.stringify(no),//date:{"":"SPH"},success:function(msg){alert(msg);}})}</script> 上边是htmljs代码,下边是类publicclassNo{publicst

ASP.NET中Web API的简单实例_实用技巧

一.Web API的路由 1.在Visual Studio中新建MVC4项目,在App_Start目录下有一个WebApiConfig.cs文件,这个文件中就是相应的Web API的路由配置了. 2.Web API 框架默认是基于 Restful 架构模式的,与ASP.NET MVC 有区别的是,它会根据 Http 请求的 HttpMethod(Get.Post.Put.Delete)来在Controller 中查找 Action,规则是:Action 名中是否以Get.Post 开头?Acti

ASP.NET(C#) Web Api通过文件流下载文件的实例_实用技巧

下载文件到本地是很多项目开发中需要实现的一个很简单的功能.说简单,是从具体的代码实现上来说的,.NET的文件下载方式有很多种,本示例给大家介绍的是ASP.NET Web Api方式返回HttpResponseMessage下载文件到本地.实现的方法很简单,其中就是读取服务器的指定路径文件流,将其做为返回的HttpResponseMessage的Content.直接贴出DownloadController控件器的代码: using System; using System.Collections.

asp.net web api 接收不到post数据问题

问题描述 asp.net web api 接收不到post数据问题 接口文档: 我自己写的代码: public class Input { public string Appkey { get; set; } public string Sno { get; set; } public string[] Clist { get; set; } } public class Rback { public string Code { get; set; } public string Msg { g

Asp.Net Web API 2第五课——Web API路由

原文:Asp.Net Web API 2第五课--Web API路由 Asp.Net Web API 导航   Asp.Net Web API第一课--入门 http://www.cnblogs.com/aehyok/p/3432158.html       Asp.Net Web API第二课--CRUD操作 http://www.cnblogs.com/aehyok/p/3434578.html       Asp.Net Web API第三课--.NET客户端调用Web API http:

【ASP.NET Web API教程】4.1 ASP.NET Web API中的路由

原文:[ASP.NET Web API教程]4.1 ASP.NET Web API中的路由 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 4.1 Routing in ASP.NET Web API 4.1 ASP.NET Web API中的路由 本文引自:http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api