- Install mongodb Driver on the ASP.Net Core Web API
- Add Connection string as follows
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"MongoDatabase": {
"ConnectionString": "mongodb://localhost:[portNumber]",
"DatabaseName": "[Database Name]",
"CollectionName": "[Table Name]"
}
}
- Create Configration class as follows
namespace MongDbWebAPI.Configration
{
public class DatabaseSettings
{
public string ConnectionString { get; set; } = string.Empty;
public string DatabaseName { get; set; } = string.Empty;
public string CollectionName { get; set; } = string.Empty;
}
}
- Add connection string to program.cs
builder.Services.Configure<DatabaseSettings>(builder.Configuration.GetSection("MongoDatabase"));