In this course you will learn how to integrate mongodb in asp net core web api. First we configure
and setup MongoDB for .Net then we will do the .Net Part inside Visual Studio.
I also have a full course on YouTube about integrating MongoDB in ASP .Net Core Web API. The
course is a full stack CRUD Application Project with React.js, MongoDB and Web API so you learn
React and Web API as well.
You can watch the full course or the video tutorial Here
In just 9 steps you can configure and setup Web API for making successful API calls to MongoDB Cloud
Database:
Configure MongoDB Cluster.
Create a new Project if you have not created yet (You can also use any other projects).
Click on the project that you want to connect to Web API then create a cluster by clicking
on The big button "Build a Database":
Select the correct options
Select the correct provider
Select the correct region
Give a good Cluster name
Give a good username and password
Select the correct environment type
Add the correct IP address. It also automatically detects the IP address of the machine
with which you configure MongoDB But if you use the MongoDB in another machine then you
have to add the IP address of that machine as well.
Click on the "Connect" button inside your MongoDB Cluster to configure the MongoDB Cluster
Select the driver(C# .Net)
Select the latest version
Copy the connection string and replace the "<password>" with the actual user
Password
Create Database
Click on the tab button "Collections"
Click on the button "Add My Own Data"
Give a Database name and Collection name (You need these names later when you setup Web
API)
When you have Database and collection(basically Table), you can add fields(columns) to
your collection or table.
By Default your collection has only on field(that is the "_id")
Add new fields by clicking on "Insert Document"
Choose the correct data type for your fields
Now you are done in MongoDB, let's jump to the Visual Studio
Create a new project if you do not have any(it can be only Web API project or
combination of Web
API and other types like MVC or React.js frontend,...)
Add a new object inside "appsettings.json" to store the MongoDB data:
Add middleware for mongodb in "Program.cs":
The Code For "DatabaseSettings.cs":
The Code For "Student.cs". Student is for example the name of your collection
inside MongoDB and
based on your collection you need to create a model in Web API:
The Code For "StudentServices.cs". Student is for example the name of your
collection inside
MongoDB. It is best practice to keep everything separated(one service for one collection):
The Code For "StudentController.cs". Student is for example the name of your
collection inside
MongoDB. It is best practice to keep everything separated(one controller for one collection):