Guest User

Untitled

a guest
May 21st, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
  2.  
  3. namespace MyLibraryOfBooks.Models{
  4. public class Book{
  5. public int Id { get; set; }
  6. public string Name { get; set; }
  7.  
  8. public int? GenreId { get; set; }
  9. public virtual Genre Genre { get; set; }
  10. }}
  11.  
  12. namespace MyLibraryOfBooks.Models{
  13. public class Genre{
  14. public int Id { get; set; }
  15. public string Name { get; set; }
  16.  
  17. public virtual ICollection<Book> Books { get; set; }
  18. }}
  19.  
  20. namespace MyLibraryOfBooks.Models{
  21. public class AppDatabaseContext : DbContext{
  22. public DbSet<Book> Books { get; set; }
  23. public DbSet<Genre> Genres { get; set; }
  24. public AppDatabaseContext(){}
  25. public AppDatabaseContext(DbContextOptions<AppDatabaseContext> options) : base(options){
  26. Database.EnsureCreated())
  27. }
  28. protected override void OnModelCreating(ModelBuilder modelBuilder){
  29. modelBuilder.Entity<Book>()
  30. .HasOne(book => book.Genre)
  31. .WithMany(genre => genre.Books)
  32. .HasForeignKey(book => book.GenreId);
  33. }}}
  34.  
  35. namespace MyLibraryOfBooks.Controllers{
  36. [Route("[controller]")]
  37. public class BooksController : Controller{
  38. private readonly AppDatabaseContext _context;
  39. public BooksController(AppDatabaseContext context) { _context = context; }
  40. [HttpGet]
  41. public IEnumerable<Book> Get(){
  42. return _context.Books.Include(book => book.Genre);
  43. }
  44. [HttpGet("{id}"]
  45. public IIncludableQueryable<Book, Genre> Get(int id){
  46. return _context.Books.Where(book=>book.Id == id).Include(book =>book.Genre);
  47. }
  48. [HttpPost]
  49. public void Post([FromBody] Book book){
  50. _context.Books.Add(book);
  51. _context.SaveChanges();
  52. }
  53. [HttpPut("{id}")]
  54. public void Put(int id, [FromBody] Book book){
  55. if (id != book.Id) return;
  56. _context.Entry(book).State = EntityState.Modified;
  57. _context.SaveChanges();
  58. }
  59. [HttpDelete("{id}")]
  60. public void Delete(int id){
  61. _context.Books.Remove(_context.Books.Find(id));
  62. _context.SaveChanges();
  63. }
  64. }}
  65.  
  66. namespace MyLibraryOfBooks.Controllers{
  67. [Route("[controller]")]
  68. public class GenresController : Controller{
  69. private readonly AppDatabaseContext _context;
  70. public GenresController(AppDatabaseContext context){
  71. _context = context;
  72. }
  73. [HttpGet]
  74. public IEnumerable<Genre> Get(){
  75. return _context.Genres.Include(genre => genre.Books);
  76. }
  77. [HttpGet("{id}")]
  78. public IQueryable<Genre> Get(int id){
  79. var genre1=_context.Genres.Where(genre=>genre.Id == id).Include(genre=>genre.Books);
  80. return genre1;
  81. }
  82. [HttpPost]
  83. public void Post([FromBody] Genre genre){
  84. _context.Genres.Add(genre);
  85. _context.SaveChanges();
  86. }
  87. [HttpPut("{id}")]
  88. public void Put(int id, [FromBody] Genre genre){
  89. if (id == genre.Id) return;
  90. _context.Entry(genre).State = EntityState.Modified;
  91. _context.SaveChanges();
  92. }
  93. [HttpDelete("{id}")]
  94. public void Delete(int id){
  95. _context.Genres.Remove(_context.Genres.Find(id));
  96. _context.SaveChanges();
  97. }
  98. }}
  99.  
  100. namespace MyLibraryOfBooks{
  101. public class Startup{
  102. public Startup(IConfiguration configuration){
  103. Configuration = configuration;
  104. }
  105. private IConfiguration Configuration { get; }
  106. public void ConfigureServices(IServiceCollection services){
  107. services.AddCors(options =>{
  108. options.AddPolicy("CorsPolicy",
  109. builder => builder.AllowAnyOrigin()
  110. .AllowAnyMethod()
  111. .AllowAnyHeader()
  112. .AllowCredentials());
  113. options.AddPolicy("AllowSpecificOrigin", policy => policy.WithOrigins("http://localhost:5000"));
  114. options.AddPolicy("AllowGetMethod", policy => policy.WithMethods("GET"));
  115. });
  116. services.Configure < MvcOptions > (options => {
  117. options.Filters.Add(new CorsAuthorizationFilterFactory("CorsPolicy"));
  118. });
  119. services.AddDbContext<AppDatabaseContext>(options =>
  120. options.UseMySQL("server=localhost;port=3306;UserId=root;Password=;database=usersdb7;SslMode=none"));
  121. services.AddMvc();
  122. }
  123. public void Configure(IApplicationBuilder app, IHostingEnvironment env){
  124. app.UseCors("CorsPolicy");
  125. if (env.IsDevelopment()){
  126. app.UseDeveloperExceptionPage();
  127. }
  128. app.UseMvc();
  129. }
  130. }}
Add Comment
Please, Sign In to add comment