[英]C# REST API - returning an array as JSON
I'm trying to build a REST API.我正在尝试构建 REST API。 I have been using this guide by Microsoft Docs and I'd appreciate some help.
我一直在使用 Microsoft Docs 的本指南,希望能得到一些帮助。
I have 2 models Library and Book.我有 2 个模型库和书籍。 Each have their own controllers as well.
每个人都有自己的控制器。
I want each to reference each other so I can get all books within a library and I want a book to reference what library it belongs to.我希望每个人都能相互引用,这样我就可以获取图书馆中的所有书籍,并且我想要一本书来引用它所属的图书馆。 I am using an in-memory database by Microsoft Entity Framework
我正在使用 Microsoft Entity Framework 的内存数据库
My current model classes look like this:我当前的 model 类如下所示:
Library:图书馆:
public class Library
{
[Key]
public long id { get; set; }
public Book[] bookArray { get; set; }
public string postalCode { get; set; }
public string street { get; set; }
public string city { get; set; }
public string country { get; set; }
}
Book:书:
public class Book
{
[Key]
public long id { get; set; }
public long libraryId { get; set; }
public string title { get; set; }
public string author { get; set; }
public string description { get; set; }
}
I want a GET endpoint like so "api/Libraries/{id}/books" that will return the array of books within a library as JSON, but I can't return the array.我想要一个像“api/Libraries/{id}/books”这样的 GET 端点,它将图书馆内的书籍数组返回为 JSON,但我无法返回该数组。 I get the error "Can't implicitly convert Models.Book to Microsoft.AspNetCore.Mvc.ActionResult<A2.Models.Library>".
我收到错误“无法将 Models.Book 隐式转换为 Microsoft.AspNetCore.Mvc.ActionResult<A2.Models.Library>”。 Have I setup the model classes correctly?
我是否正确设置了 model 类? and how do I resolve this error.
以及如何解决此错误。
The Code:编码:
// GET: api/Libraries/5/books
[HttpGet("{id}/books")]
public async Task<ActionResult<Library>> GetLibraryBooks(long id)
{
var library = await _context.Libraries.FindAsync(id);
if (library == null)
{
return NotFound();
}
return library.bookArray;
}
Your Method should return Book[]
like this:您的方法应该像这样返回
Book[]
:
[HttpGet("{id}/books")]
public async Task<ActionResult<Book[]>> GetLibraryBooks(long id)
{
var library = await _context.Libraries.FindAsync(id);
if (library == null)
{
return NotFound();
}
return Ok(library.bookArray);
}
UPDATE更新
public class Library
{
public Libary(){
books = new List<Book>();
}
[Key]
public long id { get; set; }
public List<Book> books { get; set; }
public string postalCode { get; set; }
public string street { get; set; }
public string city { get; set; }
public string country { get; set; }
}
UPDATE 2更新 2
public class LibraryController : Controller
{
private readonly LibraryContext _context;
public LibraryController(LibraryContext context)
{
_context = context;
}
[HttpPost("{id}")]
public IActionResult AddBookToLibrary([FromRoute]long id ,[FromBody] Book bookToAdd)
{
var libraryToAddBook = _context.Libraries.Include(l => l.books)
.FirstOrDefault(l => l.id == id);
if (libraryToAddBook == null)
return NotFound();
libraryToAddBook.books.Add(bookToAdd);
_context.SaveChanges();
return Ok();
}
}
UPDATED CONTEXT更新的上下文
public class LibraryContext : DbContext
{
public LibraryContext(DbContextOptions<LibraryContext> options)
: base(options)
{
}
public DbSet<Library> Libraries { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Library>()
.OwnsMany<Book>(l => l.books);
}
}
startup.cs启动.cs
var connectionString = Configuration.GetConnectionString("myDatabaseConnectionString");
services.AddDbContext<LibraryContext>(options =>
{
//options.USEYOURDATABASE(connectionString); //you might need install a NuGet eg. Microsoft.EntityFrameworkCore.SqlServer
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.