This quickstart is built as a template to be installed using the dotnet CLI. You can install the template locally after cloning this repository using the following commands. The template will also be pushed to NuGet in the near future.
Install the template from the root folder of this project.
dotnet new -i .\
Once the template is installed, create a new project.
cd preferred\location\of\project
dotnet new cosmosmongo-webapi
Follow these instructions to create a database account.
Modify the appsettings.json
file to match your configuration. Enter your connection string and modify the database and collection names if you created them with different names.
Find your connection string by selecting your Cosmos DB account in the Azure portal. Click Connection String in the left navigation, and then click Read-write Keys. Use the copy buttons on the right side of the screen to copy the Primary Connection String.
"DatabaseSettings": {
"MongoConnectionString": "<enter connection string here>",
"DatabaseName": "cosmicworks",
"ProductCollectionName": "products"
}
Use mongoimport to load the sample products provided in the Data
folder of this project. Mongoimport is a command line tool that allows you to easily import small amounts of JSON, CSV or TSV data. Download the tool and run the following from the root folder to load the sample data. Update the host, port, username, and password with values from the Connection String left navigation blade in the Azure portal. Be sure to also update the database and collection names if you named yours differently.
mongoimport --host <HOST>:<PORT> -u <USERNAME> -p <PASSWORD> --db cosmicworks --collection products --ssl --jsonArray --writeConcern="{w:0}" --file Data/products.json
Note: If you would like to skip this step you can create documents with the correct schema via the POST endpoint provided in this project.
Enter the following at the command line from the root directory of the project.
dotnet run
After the application is running, navigate to https://localhost:5001/swagger/index.html to see the swagger documentation for the web api and to submit sample requests.
Click on the API you would like to test and select "Try it out".