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 0000000000000000000000000000000000000000..ffcdc360abd7f956fc21d88ec9158f3594866f4e --- /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 0000000000000000000000000000000000000000..e76a0b05703654f4643416e1ff5fe3c1a8656fde --- /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 +~~~ + 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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..5e4013033c782e25efc338de3daca1ee61c5c9bc --- /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 0000000000000000000000000000000000000000..ab6cf044d55195e2f5cb5e08efa215107441b52c --- /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 0000000000000000000000000000000000000000..b6326b82662144ef91518395502fd52b99c6216b --- /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 0000000000000000000000000000000000000000..2669a5f109638d17343539bbb2fcc12b90adeedb --- /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 0000000000000000000000000000000000000000..7aed43f371ef098e34a5adb276df323d26b30539 --- /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 0000000000000000000000000000000000000000..a5c608a86acf261de7fa03fb0c85074b1a4dd342 --- /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/BookApi.http" "b/\351\203\221\345\256\266\347\202\234/sdad/BookApi.http" new file mode 100644 index 0000000000000000000000000000000000000000..24a52868a79e692a7e4c6114513a61441079ed0d --- /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 + + +### 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 0000000000000000000000000000000000000000..c596361214d78fa3163c1cba8553eb17e739764b --- /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 0000000000000000000000000000000000000000..29af321282ad717e679e43436ead36f997731952 --- /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 0000000000000000000000000000000000000000..b32b3b70f2fae48512041eabb10a4e36351bb79b --- /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": "*" +}