diff --git "a/\345\224\220\345\255\235\345\235\232/20240617_\350\267\250\345\237\237.md" "b/\345\224\220\345\255\235\345\235\232/20240617_\350\267\250\345\237\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..ffcdc360abd7f956fc21d88ec9158f3594866f4e --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/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/\345\224\220\345\255\235\345\235\232/20240619_\345\215\232\345\256\242\347\273\203\344\271\240.md" "b/\345\224\220\345\255\235\345\235\232/20240619_\345\215\232\345\256\242\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..e76a0b05703654f4643416e1ff5fe3c1a8656fde --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/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 +~~~ + diff --git "a/\345\224\220\345\255\235\345\235\232/pass/App.vue" "b/\345\224\220\345\255\235\345\235\232/pass/App.vue" new file mode 100644 index 0000000000000000000000000000000000000000..c0f988cb7f3029f87ba48daf679ffb305c79ed76 --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/App.vue" @@ -0,0 +1,75 @@ + + + \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/BlogApi.http" "b/\345\224\220\345\255\235\345\235\232/pass/BlogApi.http" new file mode 100644 index 0000000000000000000000000000000000000000..9db20a7e5e5eaf593b6389036873957592b55897 --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/BlogApi.http" @@ -0,0 +1,39 @@ +@BlogApi_HostAddress = http://localhost:3000 + +###根据id获取书籍信息l +GET {{BlogApi_HostAddress}}/api/blog/10 + + +###获取全部书籍信息 +GET {{BlogApi_HostAddress}}/api/blog + + +###添加书籍 +POST {{BlogApi_HostAddress}}/api/blog +Content-Type: application/json + +{ + "title": "西游", + "author": "吴承恩", + "tag":"小说" +} + + + +###更新书籍 +PUT {{BlogApi_HostAddress}}/api/blog/13 +Content-Type: application/json + +{ + "id": 13, + "title": "红楼", + "author": "吴承恩", + "tag":"小说" +} + + +###删除书籍 +DELETE {{BlogApi_HostAddress}}/api/blog/9 + + +### diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Controllers/BlogController.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Controllers/BlogController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..a59c30a1f3be2aa9a962d9111797a9895b7870ee --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Controllers/BlogController.cs" @@ -0,0 +1,56 @@ +using Microsoft.AspNetCore.Mvc; +using BlogApi.Interfaces; +using BlogApi.Models; +[Route("api/[controller]")] +[ApiController] +public class BlogController:ControllerBase{ + private readonly IBlogsRepository _blogRepository; + public BlogController(IBlogsRepository blogRepository){ + _blogRepository = blogRepository; + } + [HttpGet] + public IActionResult Get(){ + var blogs = _blogRepository.GetList(); + return Ok(blogs); + } + [HttpGet("{id}")] + public IActionResult GetById(int id){ + var blog = _blogRepository.GetById(id); + if(blog==null){ + return NotFound(); + } + return Ok(blog); + } + [HttpPost] + public IActionResult Post(Blog blog){ + _blogRepository.Add(blog); + return CreatedAtAction(nameof(GetById), new { id = blog.Id }, blog); + } + [HttpPut("{id}")] + public IActionResult Put(int id,Blog blog){ + if(id!=blog.Id){ + return BadRequest(); + } + _blogRepository.UpDate(blog); + return NoContent(); + } + [HttpDelete("{id}")] + public IActionResult Delete(int id){ + var blog = _blogRepository.GetById(id); + if(blog==null){ + return NotFound(); + } + _blogRepository.Delete(blog); + return NoContent(); + } + [HttpGet("search")] + public IActionResult Search(string keyword) + { + var blogs = _blogRepository.GetList().Where(b => b.Title.Contains(keyword) || b.Author.Contains(keyword) || b.Tag.Contains(keyword)); + if (blogs.Count() == 0) + { + return NotFound(); + } + return Ok(blogs); + } +} \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Db/BlogDbContext.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Db/BlogDbContext.cs" new file mode 100644 index 0000000000000000000000000000000000000000..46368c015b4abbb20bc12a8e14ee029a6024305b --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Db/BlogDbContext.cs" @@ -0,0 +1,8 @@ +using Microsoft.EntityFrameworkCore; +using BlogApi.Models; +namespace BlogApi.Db{ + public class BlogDbContext:DbContext{ + public BlogDbContext(DbContextOptionsoptions):base(options){} + public DbSetBlogs{ get; set; } + } +} \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Interfaces/IBlogsRepository.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Interfaces/IBlogsRepository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..093684d1bace90f9df01327c5c832c42ba911dfc --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Interfaces/IBlogsRepository.cs" @@ -0,0 +1,11 @@ +namespace BlogApi.Interfaces +{ + public interface IBlogsRepository + { + 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/\345\224\220\345\255\235\345\235\232/pass/Models/Blogs.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Models/Blogs.cs" new file mode 100644 index 0000000000000000000000000000000000000000..5dbc31adfe8c310e9a094a6d062dcb0cf5e4a123 --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Models/Blogs.cs" @@ -0,0 +1,10 @@ +namespace BlogApi.Models +{ + public class Blog + { + public int Id { get; set; } + public string Title { get; set; } = ""; + public string Author { get; set; } = ""; + public string Tag { get; set; }=""; + } +} \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Program.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..70679e7302c108a382c6a3437490d6693ca3465e --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Program.cs" @@ -0,0 +1,23 @@ +using BlogApi.Db; +using BlogApi.Models; +using BlogApi.Repository; +using BlogApi.Interfaces; +using Microsoft.EntityFrameworkCore; +//创建web程序构建器 +var builder = WebApplication.CreateBuilder(args); +//添加控制器服务 +builder.Services.AddControllers(); +//获取连接sqlserver服务器的字符串 +string sqlserverstring = builder.Configuration.GetConnectionString("sqlserver"); +//注册数据库上下文 +builder.Services.AddDbContext(options => options.UseSqlServer(sqlserverstring)); +//注册仓储接口 +builder.Services.AddScoped, BlogsRepository>(); +//构建应用程序 +var app = builder.Build(); +//配置跨域策略 +app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); +// 使用控制器服务 +app.MapControllers(); +//运行 +app.Run(); \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Repository/BlogsRepository.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Repository/BlogsRepository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..cecb91c2d12c8fa7550060ae059f89f8573b5a43 --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Repository/BlogsRepository.cs" @@ -0,0 +1,34 @@ +using BlogApi.Db; +using BlogApi.Interfaces; +using BlogApi.Models; +using Microsoft.EntityFrameworkCore; +namespace BlogApi.Repository +{ + public class BlogsRepository: IBlogsRepository{ + private readonly BlogDbContext _context; + public BlogsRepository(BlogDbContext context){ + _context = context; + } + public void Add(Blog entity){ + _context.Blogs.Add(entity); + _context.SaveChanges(); + } + public void Delete(Blog entity){ + _context.Blogs.Remove(entity); + _context.SaveChanges(); + } + + public Blog GetById(int id) + { + return _context.Blogs.Find(id) ?? throw new Exception("Blog not found"); + } + + public IEnumerable GetList(){ + return _context.Blogs.ToList(); + } + public void UpDate(Blog entity){ + _context.Entry(entity).State = EntityState.Modified; + _context.SaveChanges(); + } + } +} \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/appsettings.json" "b/\345\224\220\345\255\235\345\235\232/pass/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..95280baec5f1542aef2fbedaeed85165139a3629 --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/appsettings.json" @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "ConnectionStrings": { + "sqlserver":"server=.;Database=Blog;user=sa;password=123456;trustservercertificate=true" + }, + "AllowedHosts": "*" +}