Summary
This project focuses on creating a .NET Core Web API on macOS using Visual Studio Code, the .NET SDK, and the C# Dev Kit. The step-by-step process includes installing necessary software like the .NET SDK for building and running the application, and Visual Studio Code for writing and debugging the code. After setting up the environment, a Web API project is created using the dotnet new webapi command. The project can be run locally, providing API functionality through endpoints like the default WeatherForecast. The API can be accessed and tested using a browser or tools like Postman. Additionally, the project allows for easy debugging and expansion by adding more controllers and routes. This setup is ideal for building scalable web services with C# on a Mac.
Step 1: Install Homebrew (Optional, but recommended for managing installations)
If you don't have Homebrew installed, you can use it to manage your installations easily. Open the terminal and install it with the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Install .NET SDK
Download and Install .NET SDK:
Visit the .NET SDK Download Page and download the latest stable version for macOS.-
Alternatively, you can install it via Homebrew:
brew install --cask dotnet-sdk
Verify Installation:
After installation, confirm that the .NET SDK is correctly installed by running the following command in your terminal:
dotnet --version
Step 3: Install Visual Studio Code
Download Visual Studio Code:
Head over to the VS Code website and download the macOS version of Visual Studio Code.Install Visual Studio Code:
Once downloaded, follow the instructions to install it on your Mac.
Step 4: Install C# Dev Kit and Required Extensions
Launch VS Code.
Open the Extensions Marketplace (by clicking on the square icon on the left sidebar or pressing Cmd+Shift+X).Install C# Dev Kit:
Search for C# Dev Kit and install the extension by Microsoft.Install .NET Runtime and Debugger:
You might also need to install additional tools like .NET Core Debugger (which VS Code might prompt when you open a C# file).
Step 5: Create a New .NET Core Web API Project
Open the terminal and navigate to the folder where you want to create your project.
-
Create a new Web API project:
Run the following command to create a new .NET Core Web API project:
dotnet new webapi -n MyWebApi
Replace MyWebApi with your desired project name.
Navigate to the project directory:
cd MyWebApi
Step 6: Open the Project in Visual Studio Code
- Open the project in VS Code by typing:
code .
This will launch Visual Studio Code in the current project directory.
- Restore NuGet Packages:
Once the project is open, restore any missing dependencies by running:
dotnet restore
Step 7: Running the Web API
Run the Web API:
In the terminal, run the following command to start the Web API:
dotnet run
Access the API:
By default, your Web API will be running on http://localhost:5000 or https://localhost:5001 or the port that the dotnet run provides.
Step 8: Test the API in Browser or Curl command
Open a browser and navigate to https://localhost:5001/swagger to view the API's Swagger documentation.
You can interact with the default WeatherForecast API that comes with the template.
Step 9: Debugging in Visual Studio Code
In VS Code, press F5 to start debugging.
VS Code will attach the debugger to your running Web API.
Step 10: Add Controllers and Routes (Next Project)
Add new API endpoints by creating new controllers inside the Controllers folder.
Top comments (0)