Getting Started - Code

Shared Project

Country.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlazorCountriesWasm.Shared
{
    public class Country
    {
        public int CountryId { get; set; }
        public string CountryName { get; set; }  = String.Empty;
    }
}

City.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlazorCountriesWasm.Shared
{
    public class City
    {
        public int CityId { get; set; }
        public string CityName { get; set; } = string.Empty;
        public int CityPopulation { get; set; } = 0;
        public int CountryId { get; set; }
    }
}

Server Project

Controllers > CountryController.cs

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BlazorCountriesWasm.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CountryController : ControllerBase
    {
        public static List<Country> countries = new List<Country>
        {
            new Country {CountryId = 1, CountryName = "United Kingdom"},
            new Country {CountryId = 2, CountryName ="France"}
        };

        [HttpGet]
        public async Task<ActionResult<List<Country>>> GetCountries()
        {
            return Ok(countries);
        }

        [HttpGet]
        [Route("{CountryId}")]
        //Or you can combine the two lines as: [HttpGet("{CountryId}")]
        public async Task<ActionResult<Country>> GetSingleCountry(int CountryId)
        {
            var country = countries.FirstOrDefault(c => c.CountryId == CountryId);
            if (country == null)
            {
                return NotFound("Sorry, no country found.");
            }
            else
            {
                return Ok(country);
            }
        }

    }
}

Controllers > CityController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlazorCountriesWasm.Shared
{
    public class City
    {
        public int CityId { get; set; }
        public string CityName { get; set; } = string.Empty;
        public int CityPopulation { get; set; } = 0;
        public int CountryId { get; set; }
    }
}

Program.cs

global using BlazorCountriesWasm.Shared;
using Microsoft.AspNetCore.ResponseCompression;


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseWebAssemblyDebugging();
}
else
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();


app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");

app.Run();