Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning ASP.NET Core MVC Programming

You're reading from  Learning ASP.NET Core MVC Programming

Product type Book
Published in Nov 2016
Publisher Packt
ISBN-13 9781786463838
Pages 326 pages
Edition 1st Edition
Languages
Authors (2):
Mugilan T. S. Ragupathi Mugilan T. S. Ragupathi
Profile icon Mugilan T. S. Ragupathi
Anuraj Parameswaran Anuraj Parameswaran
Profile icon Anuraj Parameswaran
View More author details

Table of Contents (18) Chapters

Learning ASP.NET Core MVC Programming
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Introduction to ASP.NET Core 2. Setting Up the Environment 3. Controllers 4. Views 5. Models 6. Validation 7. Routing 8. Beautifying ASP.NET MVC Applications with Bootstrap 9. Deployment of ASP.NET Core Application 10. Building HTTP-based Web Services Using ASP.NET Web API 11. Improving Performance of an ASP.NET Core Application 12. ASP.NET Core Identity

Chapter 10. Building HTTP-based Web Services Using ASP.NET Web API

So far, we have learned how to create web applications using ASP.NET Core. But there are times when simply creating a web application is not enough. Let's assume you are using ASP.NET Core to create a web application that provides weather information for all the cities across the world. People access your web application to find out weather information, and they are satisfied with the service. But this weather information may be needed by many other websites or web applications, such as tourism websites, news websites, and many other mobile applications.

Instead of writing the code all over again for their websites, you can create and publish the web services and the websites can consume the required web services whenever they need to.

In this chapter, you are going to learn about the following topics:

  • What an HTTP-based service is and how it is useful

  • What Fiddler is

  • How to compose an HTTP request using Fiddler and fire the same...

HTTP basics


HTTP is a powerful platform for building services. You can use the existing HTTP verbs to build services. For example, you can use the existing HTTP verb GET to get the list of products or POST to update information about the product. Let's take a quick look at how HTTP works with respect to building the services.

There is no difference in the underlying mechanism between serving the HTML pages in ASP.NET MVC and serving the data in the context of HTTP services. Both follow a request-response pattern and the same routing mechanism.

An HTTP request can be sent from any client (desktop, laptop, tablet, mobile, and so on) to the server and the server will respond back with an HTTP response. An HTTP response can be sent to the client in any format such as JSON or XML. This is shown in the following figure:

In the preceding diagram, a request is sent from the desktop computer (it could equally be sent from a mobile or tablet; it makes no difference) and the server sends back the HTTP...

Dependency Injection


In most real-world projects, we do not instantiate any objects using the new instance in any of the Controllers, the reason being that we don't want to have tight coupling between the dependent components (between the Controller and the repository). Instead, we pass an interface to the Controller, and the Dependency Injection container (such as Unity) will create an object for us when it is needed for the Controller. This design pattern is commonly referred to as Inversion of Control.

Let's say that a class by the name of ClassA uses another class, ClassB. In this case, it is enough for ClassA to know about the behavior, methods, and properties of ClassB, and it doesn't need the internal implementation details of ClassB. So, we can abstract ClassB and make an interface out of the class, and then have that interface as the parameter instead of the concrete class. The advantage of this approach is that we can pass any class at runtime as long as it implements a commonly...

Summary


In this chapter, you learned about the HTTP service and its purpose. We discussed how to design and implement the HTTP service using Web API. We used the Fiddler tool to construct the HTTP request and get the response back. We also learned how to write the Web API method to perform CRUD operations end to end, from writing the Web API methods to firing the requests and getting the responses back.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learning ASP.NET Core MVC Programming
Published in: Nov 2016 Publisher: Packt ISBN-13: 9781786463838
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}