I searched before and have tried to implement the answers to no success.
Get in Postman for the url api/blogpage returns a 200 OK.
I am stuck, seemingly painted myself into a corner, hoping that someone points out the obvious.
Thank you all
UPDATED with new insights and working code.
Error in Chrome
Unhandled exception rendering component: '<' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
System.Text.Json.JsonException: '<' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
---> System.Text.Json.JsonReaderException: '<' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0. ```
My Razor-Component code-block
@code {
private List<BlogItem> BlogPosts = new List<BlogItem>();
protected override async Task OnAfterRenderAsync(bool firstRender)
{
BlogPosts = await Http.GetFromJsonAsync<List<BlogItem>>("GetAllBlogPosts");
}
}
**EDIT - got a working code going**
List<BlogItem> BlogPosts;
protected override async Task OnInitializedAsync()
{
var response = await Http.GetAsync("/api/GetAllBlogPosts");
if (response.IsSuccessStatusCode)
{
BlogPosts = await response.Content.ReadFromJsonAsync<List<BlogItem>>();
enter code here
My BlogController
[Route("api[controller]")]
[ApiController]
public class BlogController : ControllerBase
{
private readonly ApplicationDbContext _context;
public BlogController(ApplicationDbContext context)
{
this._context = context;
}
public List<BlogItem> BlogPosts { get; set; } = new List<BlogItem>
{
new BlogItem { Title = "My first blog", Summary = "My first blog ever"}
};
[HttpGet]
[Route("GetAllBlogPosts")]
public ActionResult<List<BlogItem>>Get()
{
return Ok(_context.BlogItem);
}
**EDIT - changed the route to this - [Route("/api/GetAllBlogPosts")]**