博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET MVC & Web API Brief Introduction
阅读量:6246 次
发布时间:2019-06-22

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

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

[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
(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
(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
(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.

 

转载于:https://www.cnblogs.com/davidgu/p/4269329.html

你可能感兴趣的文章
为什么我要垂直对齐代码(你也要如此!)
查看>>
《ANSYS Workbench 16.0超级学习手册》——1.4 本章小结
查看>>
微软确认周二更新补丁破坏了 Windows 10 重置功能
查看>>
《Cisco防火墙》一8.4 入站NAT分析
查看>>
流处理框架 Samza 成为 Apache 基金会顶级项目
查看>>
《腾讯iOS测试实践》一一3.4 测试原则
查看>>
结对编程 VS 代码审查:对比开发者文化
查看>>
用消除重复的加密工具备份数据
查看>>
《电路分析导论(原书第12版)》一1.4.1 算法语言
查看>>
PNG 图片处理库 libpng 曝出漏洞,已初步修复
查看>>
Go 开发的 IM 和推送服务 goim
查看>>
高危漏洞预警:WordPress Core 多个高危漏洞
查看>>
《DNS与BIND(第5版)》——1.5 一定要使用DNS吗
查看>>
"挖掘机指数"告诉你不一样的中国经济
查看>>
看麦肯锡如何分析中国城市群
查看>>
《数据分析变革:大数据时代精准决策之道》一1.4 全面看待运营型分析
查看>>
一分钟自我介绍:阿里云CDN
查看>>
《iOS 8开发指南》——第6章,第6.5节实战演练——使用模板Single View Application...
查看>>
【观点】离开了信息化,大数据就是为他人作嫁衣
查看>>
《HTML5+CSS3网页设计入门必读》——1.4 分裂:WHATWG TF
查看>>