ASP Net Rest API – Treasure Hunt

OK, we already setup a basic project with the prepared Blazor Wasm template and added Swagger. ASP Net Rest API – Swagger

Now let’s add a basic API for our Treasure Hunt.

First question is: What do we actually want to send back and forth?

For our idea of a Treasure Hunt game, the entity structure could look like:

Setup Entities

So let’s create the Entities within the class library aka. “Shared” Project and for now let’s not think about Attributes for checking.

namespace tryout_blazor_api.Shared;

public class Sight
{
  public string? Name { get; set; }
  public Location? Location { get; set; }
  public ICollection<SightFact>? Facts { get; set; }
}
namespace tryout_blazor_api.Shared;

public class Location
{
  public float? Latitude { get; set; }
  public float? Longitude { get; set; }
  public float? Altitude { get; set; }
}
namespace tryout_blazor_api.Shared;

public class SightFact
{
  public string? Fact { get; set; }
}

To access it, create a Controller that gives all Sights or just a specific when calling “{URL}/Sight”. As well as some examples.

using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using tryout_blazor_api.Shared;

namespace tryout_blazor_api.Server.Controllers;

[ApiController]
[Route("[controller]")]
public class SightController : ControllerBase
{
    static private List<Sight> sights = new () {
        new () {
            Name = "Brandenburger Tor",
            Location = new () { Latitude = 52.516272F, Longitude = 13.377722F, Altitude = 0 },
            Facts = new List<SightFact>() {
                new() { Fact = "Located in Berlin" },
                new() { Fact = "Architect: Carl Gotthard Langhans" },
                new() { Fact = "Construction started: 1788" },
                new() { Fact = "Completed: 1791" }
            }
        },
        new () {
            Name = "Siegessaeule",
            Location = new () { Latitude = 52.514444F, Longitude = 13.35F, Altitude = 0 },
            Facts = new List<SightFact>() {
                new() { Fact = "Located in Berlin" },
                new() { Fact = "Designer: Heinrich Strack" },
                new() { Fact = "Construction started: 1864" },
                new() { Fact = "Completed: 1873" }
            }
        }
    };

    private readonly ILogger<SightController> _logger;

    public SightController(ILogger<SightController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public IEnumerable<Sight> Get()
    {
        return sights;
    }

    [HttpGet]
    [Route("{id}")]
    public Sight Get(int id)
    {
        if(id >= sights.Count)
            return sights[0];
        return sights[id];
    }
}

Whats happening?

By setting Attribute [Route("[controller]")] to our Controller, the Framework automaticly creates an API Endpoint with the Name of Controller without the “Controller” postfix.

In line 11 to 32 a static List of Sights is created and filled with some examples. Note, that there’s no need anymore to repeat the datatype for instantiation. That’s a nice feature of the newer C# standard.

In Constructor, we expect an instance of logger and store the reference to it locally.

Followed by the 2 REST endpoints for get the full list of sights and a specific one.

Play with the API

When running the Application now, Swagger already shows the new Endpoints

If looking for the Sight at index 1, the Sight “Siegessaeule”, it’s location and facts are returned.

Code can be found here:
https://github.com/sukapx/tryout_blazor_api/tree/19e73ad121394a8495acbba7cb58f86c240f8ca2

Leave a Reply

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