Added a json file to my project in visual studio and when I run it in the browser it gets an error.
I am still very new to visual studio and c# so I do not quite understand what is wrong.
My project is called Dinosaurs This is the dinosaur.cs code
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Dinosaurs.Models
{
public class Dinosaur
{
public string DinoName { get; set; }
public string Pronounced { get; set; }
public string BriefDesc { get; set; }
public string NameMeans { get; set; }
public int LengthFt { get; set; }
public int WeightLbs { get; set; }
public int LivedYearsAgo { get; set; }
public string ClassID { get; set; }
public string PeriodID { get; set; }
public override string ToString() => JsonSerializer.Serialize<Dinosaur>(this);
}
}
So this my models.cs code:
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Dinosaurs.Models
{
public class Dinosaur
{
public string DinoName { get; set; }
public string Pronounced { get; set; }
public string BriefDesc { get; set; }
public string NameMeans { get; set; }
public int LengthFt { get; set; }
public int WeightLbs { get; set; }
public int LivedYearsAgo { get; set; }
public string ClassID { get; set; }
public string PeriodID { get; set; }
public override string ToString() => JsonSerializer.Serialize<Dinosaur>(this);
}
}
My services.cs code
using Dinosaurs.Models;
using System.Text.Json;
namespace Dinosaurs.Services
{
public class JsonDinosaurService
{
public IWebHostEnvironment WebHostEnvironment { get; }
private string JsonFileName
{
get { return Path.Combine(WebHostEnvironment.WebRootPath, "data", "dinosaurs.json"); }
}
//Constructor
public JsonDinosaurService(IWebHostEnvironment webHostEnvironment)
{
WebHostEnvironment = webHostEnvironment;
}
//Get dinosaur method
public IEnumerable<Dinosaur> GetDinosaurs()
{
using (var jsonFileReader = File.OpenText(JsonFileName))
{
return JsonSerializer.Deserialize<Dinosaur[]>(jsonFileReader.ReadToEnd(),
new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
}
}
}
}
My Startup.cs code
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Dinosaurs.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Dinosaurs
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddTransient<JsonDinosaurService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
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.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
The page I am try to run it on is List.cshtml This is the List.cshtmlcs code
using Dinosaurs.Models;
using Dinosaurs.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Dinosaurs.Pages
{
public class ListModel : PageModel
{
public JsonDinosaurService DinosaurService;
public IEnumerable<Dinosaur> Dinosaurs { get; private set; }
public ListModel(JsonDinosaurService dinosaurService)
{
DinosaurService = dinosaurService;
}
public void OnGet()
{
Dinosaurs = DinosaurService.GetDinosaurs();
}is
h }
}
Is the List.cshtml code
@page
@model Dinosaurs.Pages.ListModel
@{
}
<h1>LIST OF DINOSAURS</h1>
@foreach (var dinosaur in Model.Dinosaurs)
{
<h2>@dinosaur.DinoName</h2>
<h2>@dinosaur.Pronounced</h2>
<h2>@dinosaur.BriefDesc</h2>
<h2>@dinosaur.NameMeans</h2>
<h2>@dinosaur.LengthFt</h2>
<h2>@dinosaur.WeightLbs</h2>
<h2>@dinosaur.LivedYearsAgo</h2>
<h2>@dinosaur.ClassID</h2>
<h2>@dinosaur.PeriodID</h2>
}