ASP.Net 用户未处理核心 NullReferenceException
ASP.Net core NullReferenceException was not handled by the user
不重复:查看评论并回答这个问题。
我几乎是 ASP.net 核心开发的新手。我正在使用 MySql 作为我的数据库构建示例网站 api。
我的书本型号:
public class Book
{
[Key]
public long Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Author { get; set; }
public DateTime CreatedOn { get; set; } = DateTime.UtcNow;
public Book()
{
}
}
public class WebAPIDataContext : DbContext
{
public WebAPIDataContext(DbContextOptions<WebAPIDataContext> options)
: base(options)
{
}
public DbSet<Book> Books { get; set; }
}
图书资料库:
public class BookRepository: IBookRepository
{
private readonly WebAPIDataContext _db;
public BookRepository(WebAPIDataContext db) {
_db = db;
}
public Book CreateBook(Book book) {
_db.Books.Add(book);
_db.SaveChanges();
return book;
}
public void DeleteBook(long id)
{
Book book = GetBook(id);
if (book != null) {
_db.Books.Remove(book);
_db.SaveChanges();
}
}
public List<Book> GetAllBooks()
{
return _db.Books.AsNoTracking().ToList();
}
public Book GetBook(long id)
{
return _db.Books.FirstOrDefault(o => o.Id == id);
}
public void UpdateBook(long id, Book book)
{
throw new NotImplementedException();
}
}
public interface IBookRepository
{
List<Book> GetAllBooks();
Book GetBook(long id);
Book CreateBook(Book book);
void UpdateBook(long id, Book book);
void DeleteBook(long id);
}
和图书管理员:
[Route("api/[controller]")]
public class BooksController : Controller
{
private readonly IBookRepository _books;
[HttpGet("")]
public IActionResult GetAllBooks()
{
List<Book> books = _books.GetAllBooks();
return Ok(books);
}
[HttpGet("{id}")]
public IActionResult GetBook(long id)
{
Book book = _books.GetBook(id);
if (book == null)
{
return NotFound();
}
return Ok(book);
}
[HttpPost]
public IActionResult CreateBook([FromBody] Book book)
{
if (ModelState.IsValid == false)
{
return BadRequest(ModelState);
}
Book createdBook= _books.CreateBook(book);
return CreatedAtAction(
nameof(GetBook), new { id = createdBook.Id }, createdBook);
}
[HttpPut("{id}")]
public IActionResult UpdateBook(long id, [FromBody] Book book)
{
if (ModelState.IsValid == false)
{
return BadRequest(ModelState);
}
try
{
_books.UpdateBook(id, book);
return Ok();
}
catch (EntityNotFoundException<Book>)
{
return NotFound();
}
}
[HttpDelete("{id}")]
public IActionResult DeleteBook(long id)
{
_books.DeleteBook(id);
return Ok();
}
}
当我从 Swagger 发出 POST 创建新书的请求时,它给我 NullReferenceException
错误。我做错了什么?
要使第一个案例生效,您需要
services.AddScoped<IBookRepository, BookRepository>();
in ConfigureServices
方法位于 startup.cs
public class BooksController : Controller
{
public BooksController(IBookRepository books){
_books = books;
}
...
现在假设您在启动 class.
时将 IBookRepository 连接到 Asp.NET Core 的容器中
如果不是 那么这就是您所缺少的!
public class BooksController : Controller
{
public BooksController(){
_books = new BookRepository();
}
如果您在此之前从未使用过依赖注入,我建议您仔细阅读它,也可以通过 IOC(控制反转)进行阅读。
不重复:查看评论并回答这个问题。
我几乎是 ASP.net 核心开发的新手。我正在使用 MySql 作为我的数据库构建示例网站 api。
我的书本型号:
public class Book
{
[Key]
public long Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Author { get; set; }
public DateTime CreatedOn { get; set; } = DateTime.UtcNow;
public Book()
{
}
}
public class WebAPIDataContext : DbContext
{
public WebAPIDataContext(DbContextOptions<WebAPIDataContext> options)
: base(options)
{
}
public DbSet<Book> Books { get; set; }
}
图书资料库:
public class BookRepository: IBookRepository
{
private readonly WebAPIDataContext _db;
public BookRepository(WebAPIDataContext db) {
_db = db;
}
public Book CreateBook(Book book) {
_db.Books.Add(book);
_db.SaveChanges();
return book;
}
public void DeleteBook(long id)
{
Book book = GetBook(id);
if (book != null) {
_db.Books.Remove(book);
_db.SaveChanges();
}
}
public List<Book> GetAllBooks()
{
return _db.Books.AsNoTracking().ToList();
}
public Book GetBook(long id)
{
return _db.Books.FirstOrDefault(o => o.Id == id);
}
public void UpdateBook(long id, Book book)
{
throw new NotImplementedException();
}
}
public interface IBookRepository
{
List<Book> GetAllBooks();
Book GetBook(long id);
Book CreateBook(Book book);
void UpdateBook(long id, Book book);
void DeleteBook(long id);
}
和图书管理员:
[Route("api/[controller]")]
public class BooksController : Controller
{
private readonly IBookRepository _books;
[HttpGet("")]
public IActionResult GetAllBooks()
{
List<Book> books = _books.GetAllBooks();
return Ok(books);
}
[HttpGet("{id}")]
public IActionResult GetBook(long id)
{
Book book = _books.GetBook(id);
if (book == null)
{
return NotFound();
}
return Ok(book);
}
[HttpPost]
public IActionResult CreateBook([FromBody] Book book)
{
if (ModelState.IsValid == false)
{
return BadRequest(ModelState);
}
Book createdBook= _books.CreateBook(book);
return CreatedAtAction(
nameof(GetBook), new { id = createdBook.Id }, createdBook);
}
[HttpPut("{id}")]
public IActionResult UpdateBook(long id, [FromBody] Book book)
{
if (ModelState.IsValid == false)
{
return BadRequest(ModelState);
}
try
{
_books.UpdateBook(id, book);
return Ok();
}
catch (EntityNotFoundException<Book>)
{
return NotFound();
}
}
[HttpDelete("{id}")]
public IActionResult DeleteBook(long id)
{
_books.DeleteBook(id);
return Ok();
}
}
当我从 Swagger 发出 POST 创建新书的请求时,它给我 NullReferenceException
错误。我做错了什么?
要使第一个案例生效,您需要
services.AddScoped<IBookRepository, BookRepository>();
in ConfigureServices
方法位于 startup.cs
public class BooksController : Controller
{
public BooksController(IBookRepository books){
_books = books;
}
...
现在假设您在启动 class.
时将 IBookRepository 连接到 Asp.NET Core 的容器中如果不是 那么这就是您所缺少的!
public class BooksController : Controller
{
public BooksController(){
_books = new BookRepository();
}
如果您在此之前从未使用过依赖注入,我建议您仔细阅读它,也可以通过 IOC(控制反转)进行阅读。