From 6ed3a294fe1e88a860f049b44d27cdf0dae8f2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=93=E4=BD=8F=E4=BA=91=E6=9C=B5?= <2980290425@qq.com> Date: Mon, 17 Jun 2024 09:10:36 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20sdad?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\351\203\221\345\256\266\347\202\234/sdad/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/.keep" diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/.keep" "b/\351\203\221\345\256\266\347\202\234/sdad/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 8369a44cfb1f5456c22f8baeecb2a30053db6035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=93=E4=BD=8F=E4=BA=91=E6=9C=B5?= <2980290425@qq.com> Date: Mon, 17 Jun 2024 09:29:51 +0000 Subject: [PATCH 2/4] ee MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 抓住云朵 <2980290425@qq.com> --- .../sdad/App.vue" | 121 ++++++++++++++++++ .../sdad/BlogController.cs" | 55 ++++++++ .../sdad/BlogDbContext.cs" | 11 ++ .../sdad/BlogRepository.cs" | 43 +++++++ .../sdad/Blogapi.http" | 6 + .../sdad/Blogs.cs" | 7 + .../sdad/IRepository.cs" | 10 ++ .../sdad/Program.cs" | 37 ++++++ .../sdad/appsettings.json" | 12 ++ 9 files changed, 302 insertions(+) create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/App.vue" create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/BlogController.cs" create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/BlogDbContext.cs" create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/BlogRepository.cs" create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/Blogapi.http" create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/Blogs.cs" create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/IRepository.cs" create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/Program.cs" create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/appsettings.json" diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/App.vue" "b/\351\203\221\345\256\266\347\202\234/sdad/App.vue" new file mode 100644 index 0000000..5e40130 --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/sdad/App.vue" @@ -0,0 +1,121 @@ + + + + + diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/BlogController.cs" "b/\351\203\221\345\256\266\347\202\234/sdad/BlogController.cs" new file mode 100644 index 0000000..ab6cf04 --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/sdad/BlogController.cs" @@ -0,0 +1,55 @@ +using Microsoft.AspNetCore.Mvc; + +using Blogapi.DOmain; +using Blogapi.IRepository; +using Microsoft.Identity.Client; + + +// using Microsoft.AspNetCore.Mvc; +// using Microsoft.AspNetCore.Components;; + +[Route("api/[controller]")] +[ApiController] + +public class BlogController:ControllerBase{ + private readonly IRepository _blogRepository; + public BlogController(IRepository blogRepository){ + _blogRepository=blogRepository; + } + [HttpGet] + public IActionResult Get(){ + var blogs=_blogRepository.GetList(); + return Ok(blogs); + } + [HttpGet("{id}")] + public IActionResult GetByid(int id ){ + var blogs=_blogRepository.GetByid(id); + if(blogs==null){ + return NotFound(); + } + return Ok(blogs); + } + [HttpPost] + public IActionResult Post(Blogs blogs){ + _blogRepository.Add(blogs); + return CreatedAtAction(nameof(GetByid),new{id=blogs.Id},blogs); + + } + [HttpPut("{id}")] + public IActionResult Update(int id,Blogs blogs){ + if(id!=blogs.Id){ + return BadRequest(); + } + _blogRepository.Update(blogs); + return NoContent(); + } + [HttpDelete("{id}")] + public IActionResult Delete(int id ){ + var blogs=_blogRepository.GetByid(id); + if(blogs==null){ + return NotFound(); + } + _blogRepository.Delete(blogs); + return NoContent(); + } +} \ No newline at end of file diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/BlogDbContext.cs" "b/\351\203\221\345\256\266\347\202\234/sdad/BlogDbContext.cs" new file mode 100644 index 0000000..b6326b8 --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/sdad/BlogDbContext.cs" @@ -0,0 +1,11 @@ +using Microsoft.EntityFrameworkCore; +using Blogapi.DOmain; + +namespace Blogapi.Db; + +public class BlogDbContext:DbContext{ + public BlogDbContext(DbContextOptions options):base(options){ + + } + public DbSet Blogs{set;get;} +} \ No newline at end of file diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/BlogRepository.cs" "b/\351\203\221\345\256\266\347\202\234/sdad/BlogRepository.cs" new file mode 100644 index 0000000..2669a5f --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/sdad/BlogRepository.cs" @@ -0,0 +1,43 @@ +using Blogapi.Db; +using Blogapi.DOmain; +using Blogapi.IRepository; +using Microsoft.EntityFrameworkCore; + + +namespace Blogapi.Repository{ + public class BlogRepository : IRepository + { + private readonly BlogDbContext _context; + public BlogRepository(BlogDbContext context){ + _context=context; + } + public void Add(Blogs entity) + { + _context.Blogs.Add(entity); + _context.SaveChanges(); + } + + public void Delete(Blogs entity) + { + _context.Blogs.Remove(entity); + _context.SaveChanges(); + } + + public Blogs GetByid(int id) + { + return _context.Blogs.Find(id); + + } + + public IEnumerable GetList() + { + return _context.Blogs.ToList(); + } + + public void Update(Blogs entity) + { + _context.Entry(entity).State=EntityState.Modified; + _context.SaveChanges(); + } + } +} \ No newline at end of file diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/Blogapi.http" "b/\351\203\221\345\256\266\347\202\234/sdad/Blogapi.http" new file mode 100644 index 0000000..7aed43f --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/sdad/Blogapi.http" @@ -0,0 +1,6 @@ +@Blogapi_HostAddress = http://localhost:5063 + +GET {{Blogapi_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/Blogs.cs" "b/\351\203\221\345\256\266\347\202\234/sdad/Blogs.cs" new file mode 100644 index 0000000..a5c608a --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/sdad/Blogs.cs" @@ -0,0 +1,7 @@ +namespace Blogapi.DOmain; + +public class Blogs{ + public int Id {get;set;} + public string title {get;set;}=""; + public string author{get;set;}=""; +} \ No newline at end of file diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/IRepository.cs" "b/\351\203\221\345\256\266\347\202\234/sdad/IRepository.cs" new file mode 100644 index 0000000..c596361 --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/sdad/IRepository.cs" @@ -0,0 +1,10 @@ +namespace Blogapi.IRepository; + +public interface IRepository{ + T GetByid(int id ); + IEnumerable GetList(); + void Add(T entity); + void Update(T entity); + void Delete(T entity); + +} \ No newline at end of file diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/Program.cs" "b/\351\203\221\345\256\266\347\202\234/sdad/Program.cs" new file mode 100644 index 0000000..29af321 --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/sdad/Program.cs" @@ -0,0 +1,37 @@ +// using Microsoft.Extensions.Options; +using Microsoft.EntityFrameworkCore; +using Blogapi.Db; +using Blogapi.DOmain; +using Blogapi.IRepository; +using Blogapi.Repository; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddControllers(); +string connectionString=builder.Configuration.GetConnectionString("BlogsConnection"); +builder.Services.AddDbContext(options=>options.UseSqlServer(connectionString)); +builder.Services.AddScoped,BlogRepository>(); +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} +app.UseCors(builder=>builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod()); +app.UseHttpsRedirection(); +app.MapControllers(); + + + +app.Run(); + +record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/appsettings.json" "b/\351\203\221\345\256\266\347\202\234/sdad/appsettings.json" new file mode 100644 index 0000000..b32b3b7 --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/sdad/appsettings.json" @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "ConnectionStrings": { + "BlogsConnection":"Server=.;Database=BlogDb3;User Id=sa;password=123456;trustservercertificate=true" + }, + "AllowedHosts": "*" +} -- Gitee From f876a4654d8d9df5ad26922de8dd99866898e84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=93=E4=BD=8F=E4=BA=91=E6=9C=B5?= <2980290425@qq.com> Date: Thu, 20 Jun 2024 05:17:18 +0000 Subject: [PATCH 3/4] 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 抓住云朵 <2980290425@qq.com> --- .../sdad/BookApi.http" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "\351\203\221\345\256\266\347\202\234/sdad/BookApi.http" diff --git "a/\351\203\221\345\256\266\347\202\234/sdad/BookApi.http" "b/\351\203\221\345\256\266\347\202\234/sdad/BookApi.http" new file mode 100644 index 0000000..24a5286 --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/sdad/BookApi.http" @@ -0,0 +1,37 @@ +@Blogapi_HostAddress = http://localhost:5286 + +###根据id获取书籍信息 +GET {{Blogapi_HostAddress}}/api/blog/3 + + +###获取全部书籍信息 +GET {{Blogapi_HostAddress}}/api/blog + + +###添加书籍 +Post {{Blogapi_HostAddress}}/api/blog +Content-Type: application/json + +{ + "title": "西游记", + "author": "吴承恩" +} + + + +###更新书籍 +PUT {{Blogapi_HostAddress}}/api/blog/4 +Content-Type: application/json + +{ + "id":"4", + "title": "西游记委屈额企鹅", + "author": "吴承恩" +} + + +###删除书籍 +DELETE {{Blogapi_HostAddress}}/api/blog/2 + + +### -- Gitee From ef05d9540f765744cf7c1cf500e9ac29dd5f5846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=93=E4=BD=8F=E4=BA=91=E6=9C=B5?= <2980290425@qq.com> Date: Sun, 23 Jun 2024 13:14:22 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 抓住云朵 <2980290425@qq.com> --- .../20240617_\350\267\250\345\237\237.md" | 20 ++++++++++++++ ...32\345\256\242\347\273\203\344\271\240.md" | 26 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 "\351\203\221\345\256\266\347\202\234/20240617_\350\267\250\345\237\237.md" create mode 100644 "\351\203\221\345\256\266\347\202\234/20240619_\345\215\232\345\256\242\347\273\203\344\271\240.md" diff --git "a/\351\203\221\345\256\266\347\202\234/20240617_\350\267\250\345\237\237.md" "b/\351\203\221\345\256\266\347\202\234/20240617_\350\267\250\345\237\237.md" new file mode 100644 index 0000000..ffcdc36 --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/20240617_\350\267\250\345\237\237.md" @@ -0,0 +1,20 @@ +## 在ASP.NET Core应用程序中配置CORS(跨域资源共享)策略 +~~~c# +public void ConfigureServices(IServiceCollection services) +{ + // 添加跨域服务 + services.AddCors(options => { + // "AllowOrigin"是策略的名称,你可以根据需要命名它 + options.AddPolicy("AllowOrigin",builder => { + builder.AllowAnyOrigin() //指定允许来自任何来源的请求,WithOrigins制定对某些域名开放 + .AllowAnyMethod() //指定允许任何HTTP方法 + .AllowAnyHeader(); //指定允许任何请求头 + }); + }); +} +public void Configure(IApplicationBuilder app,IHostEnvironment env) +{ + // 使用 "AllowOrigin" 策略,要在启用路由中间件后使用 + app.UseCors("AllowOrigin"); +} +~~~ \ No newline at end of file diff --git "a/\351\203\221\345\256\266\347\202\234/20240619_\345\215\232\345\256\242\347\273\203\344\271\240.md" "b/\351\203\221\345\256\266\347\202\234/20240619_\345\215\232\345\256\242\347\273\203\344\271\240.md" new file mode 100644 index 0000000..e76a0b0 --- /dev/null +++ "b/\351\203\221\345\256\266\347\202\234/20240619_\345\215\232\345\256\242\347\273\203\344\271\240.md" @@ -0,0 +1,26 @@ +## 博客表考前梳理 +### 后端webapi +1. 配置入口文件Program.cs +2. 配置启动类,注册依赖Startup.cs +3. 创建封装表格Domain/Blog.cs +4. 创建表格数据实体类Dto:BlogCreateDto、BlogDto、BlogUpdateDto +5. 在配置文件中编写数据库连接字符串 +~~~js + "ConnectionStrings": { + "SqlServer": "server=.;database=BlogDemo;uid=sa;pwd=123456;TrustServerCertificate=true;" + } +~~~ +6. 创建数据库上下文,连接数据库Db/BlogsDbContext +7. 创建通用仓储接口Interfaces/IRepositoryBase.cs +8. 实现接口Services/RepositoryBase.cs +9. 编写控制器 +10. 生成迁移和同步数据库 +~~~js +// 全局安装dotnet-ef工具 +dotnet tool install --global dotnet-ef +// 生成迁移 +dotnet ef migrations add +// 同步数据库 +dotnet ef batadase update +~~~ + -- Gitee