In this blog, we’ll walk you through how to create a basic .NET application that simulates rolling a dice using an HTTP server. Let’s break down each step for better understanding.

Pre-requisites:

  1. Ubuntu 24.04 LTS or another supported Linux distribution.
  2. .NET 8.0 SDK installed on your machine. You can follow the instructions to install .NET SDK here.
  3. Basic knowledge of .NET and command-line usage.

Step 1: Set Up the Environment

  1. Create a new directory for your app:
sudo mkdir RollDiceApp
cd RollDiceApp

Explanation:
We first create a new directory, “RollDiceApp”, to hold all the files and code for this project. The mkdir command creates the folder, and cd changes our working directory into it, preparing us to start working on the app.

Step 2: Generate a .NET Web App

Run the following command:

sudo dotnet new web

Explanation:
This command generates a basic template for a web application using .NET. The dotnet new web command creates all the necessary files for a simple web server, including Program.cs, which we will edit next.

Step 3: Modify Program.cs

In the Program.cs file, replace the code with the following:

using System.Globalization;
using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

string HandleRollDice([FromServices] ILogger<Program> logger, string? player)
{
    var result = RollDice();

    if (string.IsNullOrEmpty(player))
    {
        logger.LogInformation("Anonymous player is rolling the dice: {result}", result);
    }
    else
    {
        logger.LogInformation("{player} is rolling the dice: {result}", player, result);
    }

    return result.ToString(CultureInfo.InvariantCulture);
}

int RollDice()
{
    return Random.Shared.Next(1, 7);
}

app.MapGet("/rolldice/{player?}", HandleRollDice);

app.Run();

Explanation:
This code defines the functionality of our app. It handles a dice roll using the HandleRollDice function. When a user visits the /rolldice route in the browser, the app generates a random number (simulating a dice roll) and logs the result. If the user specifies a player name, the log will show the player’s name; otherwise, it logs the roll for an anonymous player.

Step 4: Update launchSettings.json

Navigate to the Properties subdirectory and replace the content of launchSettings.json with the following:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:8080",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Explanation:
This file configures the project to run on http://localhost:8080. It also defines the environment as “Development” and ensures that the app will launch in your default browser when run.

Step 5: Build and Run the Application

Now that the code is in place, build the application by running:

sudo dotnet build

After building, start the server with:

sudo dotnet run

Explanation:
The dotnet build command compiles the code and prepares it for execution. Once the build succeeds, you use dotnet run to start the web server. The app will now be running on http://localhost:8080.

Step 6: Test the Application

To test the app, open your browser and visit:

http://localhost:8080/rolldice

Alternatively, you can provide a player name like this:

http://localhost:8080/rolldice/John

Explanation:
The app responds by generating a random dice roll. The URL http://localhost:8080/rolldice will show the dice roll for an anonymous player, while providing a player name in the URL (e.g., John) will log the result for that player.

Conclusion

By following these steps, you’ve successfully created a simple .NET web application that simulates rolling a dice. This project introduces you to the basics of building a .NET web app, using routes, and logging. You can expand this project by adding more features or building it into a larger system. Happy coding!

To know what is .NET please click on “.NET

For any queries contact us: Rushi-Infotech

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *