- .NET Core 3.1
- Visual Studio 2019
- C#
- Razor
- Blazor
- Web API
- JSON
- Azure
- wwwroot > data > products.json (Database)
- Components > ProductList.razor (Blazor Component)
- Controllers > ProductsController.cs (API Controller)
- Models > Product.cs (Object Model)
- Services > JSONFileProductService.cs (Data Service)
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddControllers();
services.AddTransient<JSONFileProductService>();
}
- Adds services for Razor pages to the specified IServiceCollection. AddRazorPages(), AddControllers(), AddControllersWithViews()
- Adds a transient service of the type specified in serviceType to the specified IServiceCollection. AddTransient(), ServiceLifetime
- Adds Server-Side Blazor services to the service collection. AddServerSideBlazor()
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
//endpoints.MapGet("/products", (context) =>
//{
// var products = app.ApplicationServices.GetService<JSONFileProductService>().GetProducts();
// var json = JsonSerializer.Serialize<IEnumerable<Product>>(products);
// return context.Response.WriteAsync(json);
//});
endpoints.MapControllers();
endpoints.MapBlazorHub();
});
- Endpoint Route Binder for Blazor Hub which is a base class for SignalR Hub. MapBlazorHub()
Provides information about the web hosting environment an application is running in.
Great great grand parent for all iterators be it array, list, collection etc.
Provides functionality to Serialize
objects or values to JSON (JavaScript Object Notation) or to Deserialize
JSON to objects or values
Specifies the property name that is present in the JSON when serializing and deserializing.
**[JsonPropertyName("img")]**
using (var jsonFileReader = File.OpenText(JsonFileName))
{
return JsonSerializer.Deserialize<Product[]>(jsonFileReader.ReadToEnd(),
new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
}
using (var outputStream = File.OpenWrite(JsonFileName))
{
JsonSerializer.Serialize<IEnumerable<Product>>(
new Utf8JsonWriter(outputStream, new JsonWriterOptions
{
SkipValidation = true,
Indented = true
}),
products
);
}