/API.Solution

This project is called the "Cretaceous Park API". It serves as an example project for creating an API with ASP.NET Core.

Primary LanguageC#

Example Repo for Section #6 - Building an API

By: Ryan Duff

Description

This project is called the "Cretaceous Park API". It serves as an example project for creating an API with ASP.NET Core.

Technologies Used

  • C#
  • Entity Framework Core
  • ASP.NET Core

Setup/Installation Requirements

Install Tools

Install the tools that are introduced in this series of lessons on LearnHowToProgram.com.

If you have not already, install the dotnet-ef tool by running the following command in your terminal:

dotnet tool install --global dotnet-ef --version 6.0.0

Set Up and Run Project

  1. Clone this repo.
  2. Open the terminal and navigate to this project's production directory called "CretaceousApi".
  3. Within the production directory "CretaceousApi", create two new files: appsettings.json and appsettings.Development.json.
  4. Within appsettings.json, put in the following code. Make sure to replacing the uid and pwd values in the MySQL database connection string with your own username and password for MySQL. For the LearnHowToProgram.com lessons, we always assume the uid is root and the pwd is epicodus.
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Port=3306;database=cretaceous_api;uid=[YOUR_USERNAME];pwd=[YOUR_MYSQL_PASSWORD];"
  }
}
  1. Within appsettings.Development.json, add the following code:
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Trace",
      "Microsoft.AspNetCore": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}
  1. Create the database using the migrations in the Cretaceous Park API project. Open your shell (e.g., Terminal or GitBash) to the production directory "CretaceousApi", and run dotnet ef database update. You may need to run this command for each of the branches in this repo.
  2. Within the production directory "CretaceousApi", run dotnet run in the command line to start the project server.
  3. Use your program of choice to make API calls. CORS policy is set in Program.cs and is configured to allow calls from http://localhost:3000. If you're developing a client side application to consume data from this API be sure to update the CORS policy to allow calls from the host location of the server your app is running on.

Project Roadmap, Notes and Documentation

Known Bugs

License

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Copyright (c) Ryan Duff 2023