Adding the API to Blazor
We now have a way to access JSON files stored on our filesystem. In the repo on GitHub, you can find more ways of storing our data with RavenDB or SQL server, but be mindful to keep the focus on what is important (Blazor).
Now it’s time to add the API to our Blazor Server project:
- In the BlazorServer project, add a project reference to the
Data
project. OpenProgram.cs
and add the following namespaces:using Data; using Data.Models.Interfaces;
- Add the following code:
builder.Services.AddOptions<BlogApiJsonDirectAccessSetting>() .Configure(options => { options.DataPath = @"..\..\..\Data\"; options.BlogPostsFolder = "Blogposts"; options.TagsFolder = "Tags"; options.CategoriesFolder = "Categories"; }); builder.Services.AddScoped<IBlogApi, BlogApiJsonDirectAccess>();
The snippet of code is the setting...