Building Simple API Gateways with Ocelot and ASP.net Core
In the previous article, we've discovered what is an API gateway and the cool things that it brings. This article will demonstrate how to build a simple API gateway that routes incoming HTTP requests to the appropriate downstream services.
Articles in the Series
This article belongs to a series of articles that explains the importance of API gateways and how to build them using ASP.net Core. If you're interested to learn more about API gateways, it might be a good idea to spend some time reading the articles listed below.- Part 1: API Gateway in a Nutshell.
- Part 2: Building Simple API Gateways with Ocelot.
- Part 3: API Response Aggregation using Ocelot
- Part 4: API Defense using Rate Limiting and Ocelot.
- Part 5: Containerizing API Gateways
- Part 6: Containerizing API Gateways using Alpine Base Image
Introducing Ocelot
Download POC from GithubOcelot is an open source framework used for building .NET core API gateways, the project is aimed at people using .NET / .NET Core to build applications designed with microservices or SOA architectures. Ocelot provides an easy way to write a mapping file (ocelot.json) that could be used to route incoming HTTP requests to the appropriate downstream services.
See: GITHUB | Official Website
Battle Plan
In this guide, we'll build an API gateway for an e-commerce website. The e-commerce API will contain three sub-domains that we'll call "Authentication", "Catalog" and "Ledger" contexts. The gateway we'll build in this article will behave like a reverse proxy that routes incoming HTTP requests to the following downstream services:
- Authentication Service - Bounded context in-charge of managing all authentication related things like end-users and access to system.
- Catalog Service - Bounded context in-charge of managing all offered products.
- Ledger Service - Bounded context in-charge of managing all monetary-product transactions in the system.
Setup
If you want to skip the setup phase, you can download a bunch of bash scripts that I wrote to automate the generation of the four ASP.net core projects. Once you've downloaded the scripts, place them in a folder to group the output of the automation process and execute the script named 100_build-api-gateway.sh.
If you are a newbie in ASP.net Core or just keen to learn how the setup works, you can follow the steps below:
-
Generate 4 ASP.net Core Projects
-
Generate Solution and Attach Projects
-
Install Ocelot Nuget Package
-
Generate POCOs and Controllers
You can navigate to a sample project from GITHUB and copy the following models and controllers to your solutions:-
User Model
./Authentication/Models/User.cs Github Link -
User Controller
./Authentication/Controllers/UserController.cs Github Link -
Product Model
./Catalog/Models/Product.cs Github Link -
Project Controller
./Catalog/Controllers/ProductController.cs Github Link -
Transaction Model
./Ledger/Models/Transaction.cs Github Link -
Transaction Contorller
./Ledger/Controllers/TransactionController.cs Github Link
-
-
Configure Service Ports via launchSettings.json
In order to route HTTP requests properly, you have to ensure that each service will be able to run on the following local ports:- Authentication: http://localhost:52792/
- Catalog: http://localhost:52791
- Ledger: http://localhost:52790
- Gateway: http://localhost:52793
-
Start and test individual bounded context services
Test the solutions by running each of them and issuing a GET request against the following URLS:- Authentication: http://localhost:52792/api/user
- Catalog: http://localhost:52791/api/product/
- Ledger: http://localhost:52790/api/transaction
Configuring Ocelot
Now lets focus on the most important detail which is configuring Ocelot. The first thing that we have to do is configure the ocelot.json file and add the code below. Note that the BaseUrl is required by Ocelot for it to be aware of the URL it is running in order to perform a header find & replace for certain administrative configurations.
Navigate to the Program.cs file of the Gateway project and add the following configurations on the web host builder method. The code below will setup all the middlewares required by ocelot to function.
Setting up your Routes
Now that everything we need in the gateway side is configured, its time to setup routes using the ocelot.json file. Update your ocelot.json file by updating the re-routes section. The code above setups up the mappings dictionary of (see below) your application gateway to the downstream services.
- Gateway to Authentication
- Gateway to Catalog
- Gateway to Ledger
Testing the routes out
To test the routes, start all projects and navigate to each link listed below.
- User Endpoint http://localhost:52793/api/user
- Products Endpoint http://localhost:52793/api/product
- Transaction Endpoint http://localhost:52793/api/transaction
Conclusion
In this article, we've learned how to perform basic re-routing of HTTP requests from a gateway to downstream services using Ocelot. Please do note that the article only explained basic re-routing and the power of API gateways starts to shine with request aggregation which is the subject of the next article.
Related Articles
- Part 1: API Gateway in a Nutshell.
- Part 2: Building Simple API Gateways with Ocelot.
- Part 3: API Response Aggregation using Ocelot
- Part 4: API Defense using Rate Limiting and Ocelot.
- Part 5: Containerizing API Gateways
- Part 6: Containerizing API Gateways using Alpine Base Image
- 10 Steps to Setup and Containerize an Express Server
- Microservices: Picking the .NET Framework for your containerized applications.




I think you should make a video how to build it and share such video on youtube. If you use this site https://soclikes.com you can promote your video faster
ReplyDeleteOcelot is an open-source framework for building .NET Core API gateways, particularly for applications designed around microservices or SOA architectures. It provides a mapping configuration through ocelot.json, allowing incoming HTTP requests to be routed to the appropriate downstream services.
DeleteIn the example, the API gateway is designed for an e-commerce application with three bounded contexts: Authentication, Catalog, and Ledger. Each context manages a specific area of the application, while the gateway works as a reverse proxy that routes incoming requests to the corresponding downstream service. ASP.NET Core ReactJS Full Stack Developer Course
The setup creates four ASP.NET Core projects: Authentication, Ledger, Catalog, and Gateway. After creating the solution, the Ocelot NuGet package is installed in the Gateway project, controllers and models are added for the individual services, and local ports are configured so the gateway can route requests correctly. Dot Net Full Stack Developer Course
DeleteVery informative. I advice you to post this article as a video-review on Youtube so that many interested people can see it. And you can always use the services of https://viplikes.net/buy-youtube-subscribers to increase their number.
ReplyDeleteThanks for sharing excellent information.keep sharing such useful information..You can apply Indian business visa application through Online indian evisa website.
ReplyDeleteVery nice post, I definitely love this website, keep up the good work.. yes. Azerbaijan visa for US citizens can be acquired in a breeze with the convenience of online Azerbaijan Visa application processes. The Azerbaijan government has launched a new online visa system, wherein you can fill an application and receive your Azerbaijan visa entirely online .
ReplyDeleteHello sir, Hope your evening is going well. Do you need an evisa to India? You can find more information about e visa to India on our website about India e visa.
ReplyDeleteHello friends, Indian visa for US citizens, Indian e-Visa is issued for USA citizens under the following 5 categories. More info about Indian visas you can read through our website.
ReplyDeleteperde modelleri
ReplyDeletesms onay
mobil ödeme bozdurma
nft nasıl alınır
ANKARA EVDEN EVE NAKLİYAT
Trafik Sigortası
dedektor
KURMA WEB SİTESİ
Aşk Romanları
Within these pages lies a mesmerizing journey into the realms of exploration, where curiosity knows no bounds and ideas are the currency of discovery. Can i get Bahrain visa on arrival by roadBahrain does extend visa-on-arrival services, even for road entries. Streamlining entry formalities, this eases travel logistics. Nonetheless, verify your passport's validity and adhere to entry prerequisites beforehand. With Bahrain's flexible visa protocols, discovering this lively Gulf destination becomes more convenient, whether arriving by road or other modes of transportation.
ReplyDeleteThanks for the detailed guide! For a production environment, would you recommend keeping all ReRoutes in a single Ocelot file? Travelers planning to visit Togo can explore the Togo Electronic Visa Application Portal to understand the online application process and entry requirements. Applicants should check their eligibility, prepare valid passport details, and gather the required supporting documents.
ReplyDeleteA helpful guide to building an API gateway with Ocelot and ASP.NET Core. The practical examples of routing requests between authentication, catalog, and ledger services make it easier to understand how API gateways work in a microservices architecture. Plan your business trip with a Gabon corporate entry permit online. Review the application requirements, prepare supporting documents, and submit your request ahead of travel.
ReplyDeleteThis tutorial provides a practical introduction to building API gateways with Ocelot and ASP.NET Core. I like how the e-commerce example separates authentication, catalog, and ledger services, making the routing process easier to understand. Its clear structure also reflects the value of astep by step UK ETA guide for travellers preparing their applications.
ReplyDeleteIt's a clear starting point for building a gateway, while also pointing readers toward more advanced features like response aggregation and rate limiting. Travellers visiting relatives in Cameroon can prepare their documents before starting the application. An online visa visit family Cameroon application generally requires a valid passport, an invitation letter from the host, proof of family relationship, accommodation details, return flight reservation, and a yellow fever vaccination certificate.
ReplyDeleteA practical introduction to building API gateways with Ocelot and ASP.NET Core. I like how the article breaks down the architecture into separate services for authentication, product catalogs, and transactions. The step-by-step approach makes the concept easier to follow, especially for developers exploring microservices and learning how to manage request routing efficiently. It also highlights how topics such as DR Congo tourist visa cost can be presented through well-organized online information.
ReplyDeleteRouting requests to separate services through one gateway makes microservice design far easier to manage. short stay Cameroon transit visa Ocelot mapping files keep downstream traffic organised, and splitting authentication, catalog and ledger contexts gives beginners a realistic e-commerce setup worth following.
ReplyDeleteA useful introduction to building API gateways with Ocelot and ASP.NET Core. The practical routing examples, configuration details, and downstream service setup make it easier to understand how microservices communicate through a single gateway. I would also be interested in exploring how response aggregation works in the next part of this series. Planning a last-minute trip? Learn about the fast track Egypt e-Visa process, review the requirements, and submit your application early to help avoid delays before departure.
ReplyDeleteExcellent post! This guide has made it very easy to understand microservices routing using ocelot.json and ASP.NET Core. The examples involving authentication, catalog, and ledger services are perfectly clear. Travellers can complete their Saudi Visa Application from Home through the official online portal if their nationality is eligible. Applicants need to fill out the application form, provide valid passport details, submit the required information, and pay the visa fee. Once approved, the eVisa is sent by email, making the process convenient and simple.
ReplyDeleteRouting incoming requests through a simple JSON mapping file makes Ocelot approachable compared to building gateway logic from scratch. If a developer conference or remote contract ever takes you to southern Africa, check the digital passport photo specs Mozambique requires so your e-visa isn't rejected over the picture. Breaking this series into separate parts, covering aggregation and rate limiting individually, helps developers digest microservices concepts without feeling overwhelmed by ASP.NET Core complexity.
ReplyDeleteBuilding a successful career and developing reliable technology solutions both require careful planning and the right preparation. This article explains how to create an ASP.NET Core API gateway using Ocelot, connecting authentication, catalog, and ledger services through configured routes. Similarly, professionals planning to work in Morocco should understand the Long-term employment visa Morocco process, including eligibility, employment details, and required documents. Whether developing software or preparing for a new career abroad, following clear steps and organizing everything in advance can make the experience smoother, more efficient, and rewarding.
ReplyDeleteThis is a helpful tutorial for developers learning to build API gateways with ASP.NET Core and Ocelot. The e-commerce example makes routing between authentication, catalog, and ledger services easy to understand. Similarly, a website offering Get Indian Evisa Online services can benefit from secure API routing and efficient service integration to improve the user experience.
ReplyDeleteBuilding a simple API gateway with Ocelot in ASP.NET Core helps developers manage communication between microservices through organised routes and efficient request handling. Similarly, planning an international journey requires clear information and proper preparation. Egypt offers fascinating pyramids, historic temples, beautiful beaches, and lively markets. Taking time to check Egypt visa exemption countries helps travellers understand entry conditions based on nationality, prepare necessary documents, and determine whether a visa is required before enjoying a memorable Egyptian holiday.
ReplyDeleteThis is a great way to start understanding how API gateways can be created using Ocelot and ASP.NET Core. The detailed description of service routing, middleware configuration, and downstream endpoints is a great base for those who work with microservices architecture. I would also like to study the response aggregation methods described in the next chapter. Planning a trip to India? Review the entry requirements, prepare your documents, and explore helpful guidance for visitors with the India Travel Visa for Tourists resource!
ReplyDeleteRouting incoming requests to downstream services through Ocelot keeps ASP.NET Core microservices tidy, and building on a previous series part makes the learning path easy to follow. If your development team is travelling to India for a conference or a client project, it helps to Apply Indian Evisa from Home well before departure. Developers moving toward gateway patterns will appreciate this hands on direction.
ReplyDeleteBuilding an API gateway with Ocelot and ASP.NET Core helps connect multiple services through one entry point, making applications easier to manage through simple routing, configuration, and testing. Similarly, preparing for international travel becomes easier when important details are organised in advance. Travellers planning a visit to Saudi Arabia can learn how to Apply Saudi Visa from Home, check their eligibility, prepare passport information, and submit clear supporting documents. Whether managing digital services or arranging a journey, careful preparation and attention to detail help create a smoother experience and avoid unnecessary complications.
ReplyDeleteReally helpful tutorial! I like how the article breaks down the process of building an API gateway with Ocelot into simple, manageable steps. The e-commerce example makes the concept easier to understand, especially for developers exploring microservices. Looking forward to learning more about API aggregation in the next part! Applicants should review the Gabon entry visa document requirements before starting their application. Prepare a valid passport, recent photograph, travel itinerary, accommodation details, and any additional supporting documents required for your visa category.
ReplyDelete