ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework. In today’s post we shall be looking at the following topics:
This article is an extract from the book Mastering ASP.NET Web API written by Mithun Pattankar and Malendra Hurbuns.
Model-View-Controller (MVC) is a powerful and elegant way of separating concerns within an application and applies itself extremely well to web applications.
With ASP.NETMVC, it's translated roughly as follows:
Looking back to days when ASP.NETASMX-based XML web service was widely used for building service-oriented applications, it was easiest way to create SOAP-based service which can be used by both .NET applications and non .NET applications. It was available only over HTTP.
Around 2006, Microsoft released Windows Communication Foundation (WCF).WCF was and even now a powerful technology for building SOA-based applications. It was giant leap in the world of Microsoft .NET world.
WCF was flexible enough to be configured as HTTP service, Remoting service, TCP service, and so on. Using Contracts of WCF, we would keep entire business logic code base same and expose the service as HTTP based or non HTTP based via SOAP/ non SOAP.
Until 2010 the ASMX based XML web service or WCF service were widely used in client server based applications, in-fact everything was running smoothly.
But the developers of .NET or non .NET community started to feel need for completely new SOA technology for client server applications. Some of reasons behind them were as follows:
Any developer who has seen evolving nature of SOA based technologies like ASMX, WCF or any SOAP based felt the need to have much lighter, HTTP based services.
HTTP only, JSON compatible POCO based lightweight services was need of the hour and concept of Web API started gaining momentum.
A Web API is a programmatic interface to a system that is accessed via standard HTTP methods and headers. A Web API can be accessed by a variety of HTTP clients, including browsers and mobile devices.
For Web API to be successful HTTP based service, it needed strong web infrastructure like hosting, caching, concurrency, logging, security etc. One of the best web infrastructure was none other than ASP.NET.
ASP.NET either in form Web Form or MVC was widely adopted, so the solid base for web infrastructure was mature enough to be extended as Web API.
Microsoft responded to community needs by creating ASP.NET Web API- a super-simple yet very powerful framework for building HTTP-only, JSON-by-default web services without all the fuss of WCF.
ASP.NET Web API can be used to build REST based services in matter of minutes and can easily consumed with any front end technologies. It used IIS (mostly) for hosting, caching, concurrency etc. features, it became quite popular.
It was launched in 2012 with most basics needs for HTTP based services like convention-based Routing, HTTP Request and Response messages.
Later Microsoft released much bigger and better ASP.NET Web API 2 along with ASP.NETMVC 5 in Visual Studio 2013.
ASP.NET Web API 2 evolved at much faster pace with these features.
Installing of Web API 2 was made simpler by using NuGet, either create empty ASP.NET or MVC project and then run command in NuGet Package Manager Console:
Install-Package Microsoft.AspNet.WebApi
Initial release of Web API was based on convention-based routing meaning we define one or more route templates and work around it. It's simple without much fuss as routing logic in a single place & it's applied across all controllers.
The real world applications are more complicated with resources (controllers/ actions) have child resources like customers having orders, books having authors etc. In such cases convention-based routing is not scalable.
Web API 2 introduced a new concept of Attribute Routing which uses attributes in programming languages to define routes. One straight forward advantage is developer has full controls how URIs for Web API are formed.
Here is quick snippet of Attribute Routing:
[Route("customers/{customerId}/orders")]
public IEnumerable<Order>GetOrdersByCustomer(intcustomerId) { ... }
For more understanding on this, read Attribute Routing in ASP.NET Web API 2 (https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2)
ASP.NET Web API lives on ASP.NET framework, leading to think that it can be hosted on IIS only. The Web API 2 came new hosting package.
With this package it can self-hosted outside IIS using OWIN/Katana.
Any Web API developed either using .NET or non .NET technologies and meant to be used across different web frameworks, then enabling CORS is must.
A must read on CORS&ASP.NET Web API 2 (https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api).
IHTTPActionResult and Web API OData improvements are other few notable features which helped evolve Web API 2 as strong technology for developing HTTP based services.
ASP.NET Web API 2 has becoming more powerful over the years with C# language improvements like Asynchronous programming using Async/ Await, LINQ, Entity Framework Integration, Dependency Injection with DI frameworks, and so on.
Every technology has to evolve with growing needs and advancements in hardware, network and software industry, ASP.NET Web API is no exception to that.
Some of the evolution that ASP.NET Web API should undergo from perspectives of developer community, enterprises and end users are:
We saw that why Web APIs were incepted, how they evolved into powerful HTTP based service and some evolutions required. With these thoughts Microsoft made an entry into world of Open Source by launching .NET Core and ASP.NET Core 1.0.
.NET Core is a cross-platform free and open-source managed software framework similar to .NET Framework. It consists of CoreCLR, a complete cross-platform runtime implementation of CLR.
.NET Core 1.0 was released on 27 June 2016 along with Visual Studio 2015 Update 3, which enables .NET Core development.
In much simpler terms .NET Core applications can be developed, tested, deployed on cross platforms such as Windows, Linux flavours, macOS systems.
With help of .NET Core, we don't really need Windows OS and in particular Visual Studio IDE to develop ASP.NET web applications, command-line apps, libraries, and UWP apps.
In short, let's understand .NET Core components:
.NET Core shares subset of original .NET framework, plus it comes with its own set of APIs that is not part of .NET framework. This results in some shared APIs that can be used by both .NET core and .NET framework.
A .Net Core application can easily work on existing .NET Framework but not vice versa.
.NET Core provides a CLI (Command Line Interface) for an execution entry point for operating systems and provides developer services like compilation and package management.
The following are the .NET Core interesting points to know:
.NET Core supports four cross-platform scenarios--ASP.NET Core web apps, command-line apps, libraries, and Universal Windows Platform apps.
It does not implement Windows Forms or WPF which render the standard GUI for desktop software on Windows.
At present only C# programming language can be used to write .NET Core apps. F# and VB support are on the way.
We will primarily focus on ASP.NET Core web apps which includes MVC and Web API. CLI apps, libraries will be covered briefly.
A new open-source and cross-platform framework for building modern cloud-based web applications using .NET.
ASP.NET Core is completely open-source, you can download it from GitHub. It's cross platform meaning you can develop ASP.NET Core apps on Linux/macOS and of course on Windows OS.
ASP.NET was first released almost 15 years back with .NET framework. Since then it's adopted by millions of developers for large, small applications. ASP.NET has evolved with many capabilities.
With .NET Core as cross platform, ASP.NET took a huge leap beyond boundaries of Windows OS environment for development and deployment of web applications.
ASP.NET Core high level overview provides following insights:
ASP.NET Core has much more foundational improvements apart from being cross-platform, we gain following advantages of using ASP.NET Core:
To summarize, we covered MVC framework and introduced .NET Core and its architecture.
You can leverage ASP.Net Web API to build professional web services and create powerful applications check out this book, Mastering ASP.NET Web API written by Mithun Pattankar and Malendra Hurbuns.
Why ASP.NET makes building apps for mobile and web easy – Interview with Jason de Oliveira
How to call an Azure function from an ASP.NET Core MVC application