Mastering ASP.NET Web API
eBook - ePub

Mastering ASP.NET Web API

Mithun Pattankar, Malendra Hurbuns

Share book
  1. 330 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering ASP.NET Web API

Mithun Pattankar, Malendra Hurbuns

Book details
Book preview
Table of contents
Citations

About This Book

Leverage ASP.Net Web API to build professional web services and create powerful applications.About This Book• Get a comprehensive analysis of the latest specification of ASP.NET Core and all the changes to the underlying platform that you need to know to make the most of the web API• See an advanced coverage of ASP.NET Core Web API to create robust models for your data, create controllers, and handle routing and security• This book is packed with key theoretical and practical concepts that can be instantly applied to build professional applications using API with Angular 4, Ionic, and React Who This Book Is ForThis book is for.Net developers who wants to Master ASP.NET Core (Web API) and have played around with previous ASP.NET Web API a little, but don't have in-depth knowledge of it. You need to know Visual Studio and C#, and have some HTML, CSS, and JavaScript knowledge.What You Will Learn• Acquire conceptual and hands-on knowledge of ASP.NET Core (MVC & Web API)• Learn about HTTP methods, the structure of HTTP content, internet media types, and how servers respond to HTTP requests and their associated HTTP codes• Explore middleware, filters, routing, and unit testing• Optimize Web API implementations• Develop a secure Web API interface• Deploy Web API projects to various platforms• Consume your web API in front end application based on Angular 4, Bootstrap, and Ionic• Implement and explore the current trends in service architectureIn DetailMicrosoft has unified their main web development platforms. This unification will help develop web applications using various pieces of the ASP.NET platform that can be deployed on both Windows and LINUX. With ASP.NET Core (Web API), it will become easier than ever to build secure HTTP services that can be used from any client.Mastering ASP.NET Web API starts with the building blocks of the ASP.NET Core, then gradually moves on to implementing various HTTP routing strategies in the Web API. We then focus on the key components of building applications that employ the Web API, such as Kestrel, Middleware, Filters, Logging, Security, and Entity Framework.Readers will be introduced to take the TDD approach to write test cases along with the new Visual Studio 2017 live unit testing feature. They will also be introduced to integrate with the database using ORMs. Finally, we explore how the Web API can be consumed in a browser as well as by mobile applications by utilizing Angular 4, Ionic and ReactJS.By the end of this book, you will be able to apply best practices to develop complex Web API, consume them in frontend applications and deploy these applications to a modern hosting infrastructure.Style and approachUsing a hands-on approach, we cover both the conceptual as well as the technical aspects of the ASP.NET Core (Web API) framework.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mastering ASP.NET Web API an online PDF/ePUB?
Yes, you can access Mastering ASP.NET Web API by Mithun Pattankar, Malendra Hurbuns in PDF and/or ePUB format, as well as other popular books in Informatica & Linguaggi di programmazione. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781786469380

Anatomy of ASP.NET Core Web API

This chapter starts with a quick recap of MVC. You might be surprised why we need MVC when we are working with web APIs. It's because the ASP.NET Web API is designed based on the MVC principles of Controllers, Models, and Views (the response returned can be treated as a faceless view in the case of a Web API).
Our focus in this chapter is to know why we need a lightweight HTTP-based service technology in the form of a Web API, its evolution to meet the constantly changing industry demands, the entry of Microsoft into the world of Open Source in the form of .NET Core and ASP.NET Core apps, and not limiting ourselves to the world of Windows OS for developing ASP.NET web applications.
In this chapter, we will be looking at the following topics:
  • A quick recap of the MVC framework
  • Inception of web APIs and their evolution
  • Introduction to .NET Core
  • An overview of the ASP.NET Core Architecture
  • Creating an ASP.NET Core Project using Visual Studio IDE
  • Creating an ASP.NET Core Project in Linux/macOS
  • Examining the ASP.NET Core project files and structures
  • Understanding Request processing
  • MVC and web API unification
  • Running the ASP.NET Core Web API

A quick recap of the MVC framework

A 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.NET MVC, MVC stands for the following:
  • Models (M): These are classes that represent the domain model. Most of them represent the data stored in a database, for example, Employee, Customer, and so on.
  • View (V): This is a dynamically generated HTML page as template.
  • Controller (C): This is a class that manages the interaction between the View and the Model. Any operation on a View should have a corresponding handling in the Controller like user inputs, render appropriate UI, Authentication, Logging, and so on.

Inception of Web APIs and their evolution

Looking back to the days when the ASP.NET ASMX-based XML web service was widely used for building service-oriented applications, it was the easiest way to create a SOAP (Simple Object Access Protocol)-based service that could be used by both .NET applications and non-.NET applications. It was available only over HTTP.
In the late 2007, Microsoft released Windows Communication Foundation (WCF). WCF was and is, even now, a powerful technology for building SOA-based applications. It was a giant leap in the world of the Microsoft .NET world.
WCF was flexible enough to be configured as an HTTP service, remoting service, TCP service, and so on. Using the contracts of WCF, we would keep the entire business logic code base the 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, was widely used in client-server-based applications; in fact, everything was running smoothly.
But the developers of the .NET and non-.NET community started to feel the need for a completely new SOA technology for client-server applications. Some of the reasons behind this were as follows:
  • With applications in production, the amount of data used while communicating started to explode, and transferring them over the network was bandwidth consuming.
  • SOAP, being lightweight to some extent, started to show signs of payload increase. A few KB SOAP packets would become a few MB of data transfer.
  • Consuming the SOAP service in applications led to huge application sizes because of WSDL and proxy generation. This was even worse when it was used in web applications.
  • Any changes to the SOAP services led to updating the service proxy to reflect changes. This wasn't an easy task for any developer.
  • JavaScript-based web frameworks were released, and gained ground for a much simpler way of web development. Consuming SOAP-based services was not that optimal.
  • Hand-held devices, like tablets and smartphones, became popular. They had more focused applications, and needed a very lightweight service-oriented approach.
  • Browser-based Single Page Applications (SPA) gained ground very rapidly. Using SOAP-based services was quite heavy for these SPA.
  • Microsoft released REST-based WCF components, which can be configured to respond in JSON or XML, but still it was built on top of heavy technology of WCF.
  • Applications where no longer just large enterprise services, and there was a need for a more focused, lightweight, and easy-to-use service, which could be up and running in a few days.
Any developer who has seen the evolving nature of SOA-based technologies such as ASMX, WCF, or any SOAP-based technology, felt the need to have much lighter, HTTP-based services.
HTTP-only, JSON-compatible POCO (Plain Old CLR Object)-based lightweight services was the need of the hour, and the concept of Web APIs started gaining momentum.

Introducing web API

Any method(s) that is accessible over the web using HTTP verbs is called a web API. It is a lightweight way of transmitting data over HTTP, easily consumed by various clients like browsers, desktop apps, handheld devices, or even other SOA applications.
For a web API to be a successful HTTP-based service, it needed a strong web infrastructure like hosting, caching, concurrency, logging, security, and so on. One of the best web infrastructures was none other than ASP.NET.
ASP.NET, either in the form of a Web Form or an MVC, was widely adopted, so the solid base for web infrastructure was mature enough to be extended as a Web API.
Microsoft responded to the 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.
The ASP.NET Web API can be used to build REST-based services in a matter of minutes, and can be easily consumed with any of the frontend technologies.
It was launched in 2012 with the most basic needs for HTTP-based services like convention-based Routing, HTTP Request, and Response messages.
Later, Microsoft released the much bigger and better ASP.NET Web API 2 along with ASP.NET MVC 5 in Visual Studio 2013.
ASP.NET Web API 2 evolved at a much faster pace with these features:
  • Installing of the web API 2 was made simpler by using NuGet; you can create either an empty ASP.NET or MVC project, and then run the following command on the NuGet Package Manager Console:
 Install-Package Microsoft.AspNet.WebApi 
  • The initial release of the web API was based on convention-based routing, which means that we define one or more route templates, and work around it. It's simple without much fuss, as the routing logic is in a single place, and it's applied across all controllers.
  • The real-world applications are more complicated with resources (controllers/ actions) having child resources, for example, customers having orders, books having authors, and so on. 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 straightforward advantage is that the developer has full control over how URIs for the web API are formed.
  • Here a is quick snippet of Attribute Routing:
 Route("customers/{customerId}/orders")] public IEnumerable<Order>GetOrdersByCustomer(int customerId) { ... } 
For more details on this, read Attribute Routing in ASP.NET Web API 2 at https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2.
  • An ASP.NET Web API lives on the ASP.NET framework, which may lead you to think that it can be hosted on IIS only. However, using OWIN self-host, it can be hosted without IIS also.
  • If any we...

Table of contents