首页 > 代码库 > Introduction to ASP.NET Web API
Introduction to ASP.NET Web API
Services for Native Apps
As stated earlier, smartphones had an important part in the trend toward simpler pure HTTP API and away from
SOAP. Many of the native apps can be likened to dumb terminals connected to a big mainframe, with the mainframe
being the cloud. In these apps, the cloud provides storage and server-side processing and the app is mainly used for
the consumption of the services exposed by an API.
If you are thinking of building an app for smartphones, you should seriously consider exposing your backend
services through a REST API using ASP.NET Web API.
Self-Hosted HTTP Server
Before ASP.NET Web API, there was no easy way to host a simple web server in your Windows service or desktop
application. If you needed to host a service, you had to use WCF, which was not ideal.
ASP.NET Web API has made it extremely easy to host your API in a nonweb server process. Most of the code
you need to write is exactly the same as the code you would write in a web-hosted scenario. You can also use the
self-hosted Web API server to serve static resources or even a server-rendered view (e.g., Razor) so you can serve
web pages. A common scenario is when you have a process running as a Windows service and you need a
web-based administration console for it, in which case the self-hosted Web API is all you need.
ASP.NET Web API and Other Architectural Aspects
In the past few years, the software world has experienced a massive change and a huge influx of ideas and practices
(cloud, devops, BDD, SPA, Big Data, etc.). ASP.NET Web API was born in a landscape of very heterogenous
architectural patterns. There are emerging trends in the community that, whether we like it or not, are going to affect
how we will be using ASP.NET Web API.
This by itself is a big subject, and we are only going to scratch the surface here, but it is important to provide
pointers relevant to the rest of this book!
ASP.NET Web API and ASP.NET MVC
As stated earlier, there is a considerable similarity between ASP.NET Web API and ASP.NET MVC. Having said that,
there is not a great deal of functional overlap between them, but a question that crops up time and time again is the
choice between these two independent frameworks.
If you have tried the ASP.NET Web API template in the Visual Studio, you have probably noticed that this template
combines ASP.NET Web API and ASP.NET MVC. In fact, nothing stops you from using both of these frameworks in the
same project. But you need to be clear about what the main value of each framework is.
ASP.NET MVC is ideal for generating server-side views (Razor or Web Forms). And in terms of processing
client-side data, it is mainly geared toward x-www-form-urlencoded data (form post data). Although ASP.NET Web API
can process x-www-form-urlencoded data and can generate Razor views, it is best for exposing an API.
If you have both these requirements in a project, use both frameworks in conjunction.
ASP.NET Web API and DDD
Since its formalization in 2003, Domain-Driven Design (DDD) has provided the vocabulary, semantics, and conceptual
tools for designing and architecting modern software. Eric Evans introduced DDD in his book Domain-Driven
Design: Tackling Complexity in the Heart of Software (Addison-Wesley 2003), which has earned classic status among
computer software books along with the Gang of Four design patterns book (Design Patterns: Elements of Reusable
Object-Oriented Software, by Erich Gamma et al., Addison-Wesley 1994) and Martin Fowler’s Patterns of Enterprise
Application Architecture (Addison-Wesley 2002).
Introduction to ASP.NET Web API