DevSight

REST API .NET 6.0, MongoDB

Intro to MongoDB with C#에 이어 이번 주제는 MongoDB와 함께 ASP.NET Core 6 REST API1를 구현하는 예제(Sample)이다.

이전과 마찬가지로 Nuget에서 MongoDB.Driver 패키지를 추가한다.

appsettings.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "UserInfoDatabaseSetting": {
    "UserInfoCollectionName": "UserInfo",
    "ConnectionString": "mongodb://<ID>:<Password>@localhost:27017/?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=testdb&authMechanism=SCRAM-SHA-256",
    "DatabaseName": "testdb"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}  
UserInfoDatabaseSetting.cs
1
2
3
4
5
6
7
8
9
10
// interface
namespace UserInfoManagement.Models
{
    public interface IUserInfoDatabaseSetting
    {
        string UserInfoCollectionName { get; set; }
        string ConnectionString { get; set; }
        string DatabaseName { get; set; }
    }
}
Read More ···