In Visual Studio 2019, i have created an project with asp.net core project / angular template. Also added a model to create a web api controller crud with EF. These are model and controller code.
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BooksWebApiAng.Models
{
public class Book
{
[Key]
public int BookId { get; set; }
[Column(TypeName = "nvarchar(40)")]
public string Titulo { get; set; }
[Column(TypeName = "nvarchar(40)")]
public string Autor { get; set; }
[Column(TypeName = "nvarchar(40)")]
public string Editorial { get; set; }
}
}
and for controller
using BooksWebApiAng.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BooksWebApiAng.Controllers
{
[Route("[controller]")]
[ApiController]
public class BooksController : ControllerBase
{
private readonly BookContext _context;
private readonly List<Book> books = new List<Book>();
public BooksController(BookContext context)
{
_context = context;
books.Add(new Book
{
BookId = 101,
Titulo = "Tit1",
Autor = "Aut1",
Editorial = "Edi1"
});
books.Add(new Book
{
BookId = 102,
Titulo = "Tit2",
Autor = "Aut2",
Editorial = "Edit2"
});
books.Add(new Book
{
BookId = 104,
Titulo = "Tit2",
Autor = "Aut3",
Editorial = "Edit3"
});
}
// Get list
[HttpGet]
public async Task<ActionResult<IEnumerable<Book>>> GetBooks()
{
var data = await _context.Books.ToListAsync();
if (data == null)
return NotFound("No record");
return (data);
}
private async Task<List<Book>> DameBooks()
{
await Task.Delay(100).ConfigureAwait(false);
return books;
}
}
Then in Visual Studio with iis express when a call http://localhost/books it return a list with all books in book table, but when i publish in IIS 10 it doesn't, it return error trying parsing home page to json. But if a change line "var data = await _context.Books.ToListAsync();" to "var data = await DameBooks(); that just return the list books it works right both in iis express and IIS 10. Really i don't have any idea what happen, because debugging in both cases "data" variable had a list with object book. Thanks
Aucun commentaire:
Enregistrer un commentaire