dotnet_learn/appsettings.json
| { |
| "Logging": { |
| "LogLevel": { |
| "Default": "Information", |
| "Microsoft.AspNetCore": "Warning" |
| } |
| }, |
| "AllowedHosts": "*" |
| } |
| |
dotnet_learn/appsettings.Development.json
| { |
| "Logging": { |
| "LogLevel": { |
| "Default": "Information", |
| "Microsoft.AspNetCore": "Warning" |
| } |
| } |
| } |
| |
dotnet_learn/dotnet_learn.csproj
| <Project Sdk="Microsoft.NET.Sdk.Web"> |
| |
| <PropertyGroup> |
| <TargetFramework>net7.0</TargetFramework> |
| <Nullable>enable</Nullable> |
| <ImplicitUsings>enable</ImplicitUsings> |
| </PropertyGroup> |
| |
| <ItemGroup> |
| <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" /> |
| <PackageReference Include="Microsoft.Data.Sqlite" Version="7.0.9" /> |
| <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0"> |
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
| <PrivateAssets>all</PrivateAssets> |
| </PackageReference> |
| <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0" /> |
| <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0" /> |
| <PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" /> |
| </ItemGroup> |
| |
| </Project> |
| |
dotnet_learn/Program.cs
| using Microsoft.EntityFrameworkCore; |
| using dotnet_learn.Models; |
| |
| var builder = WebApplication.CreateBuilder(args); |
| var connectionString = builder.Configuration.GetConnectionString("Pizzas") ?? "Data Source=Pizzas.db"; |
| |
| |
| builder.Services.AddControllers(); |
| |
| builder.Services.AddEndpointsApiExplorer(); |
| |
| |
| |
| builder.Services.AddSqlite<PizzaDb>(connectionString); |
| builder.Services.AddSwaggerGen(); |
| |
| var app = builder.Build(); |
| |
| |
| if (app.Environment.IsDevelopment()) |
| { |
| app.UseSwagger(); |
| app.UseSwaggerUI(); |
| } |
| |
| app.UseHttpsRedirection(); |
| |
| app.UseAuthorization(); |
| |
| app.MapControllers(); |
| |
| app.MapGet("/newpizzas", async (PizzaDb db) => await db.Pizzas.ToListAsync()); |
| app.MapPost("/newpizza", async (PizzaDb db, Pizza pizza) => |
| { |
| await db.Pizzas.AddAsync(pizza); |
| await db.SaveChangesAsync(); |
| return Results.Created($"/newpizza/{pizza.Id}", pizza); |
| }); |
| |
| |
| app.Run(); |
| |
dotnet_learn/models/Pizza.cs
| using Microsoft.EntityFrameworkCore; |
| namespace dotnet_learn.Models; |
| |
| public class Pizza |
| { |
| public int Id { get; set; } |
| public string? Name { get; set; } |
| public bool IsGlutenFree { get; set; } |
| |
| } |
| class PizzaDb : DbContext |
| { |
| public PizzaDb(DbContextOptions options) : base(options) { } |
| public DbSet<Pizza> Pizzas { get; set; } = null!; |
| } |
| |
dotnet_learn/Controllers/PizzaController.cs
| using dotnet_learn.Models; |
| using dotnet_learn.Services; |
| |
| using Microsoft.AspNetCore.Mvc; |
| |
| namespace ContosoPizza.Controllers; |
| |
| [ApiController] |
| [Route("[controller]")] |
| public class PizzaController : ControllerBase |
| { |
| public PizzaController() |
| { |
| } |
| |
| |
| [HttpGet] |
| public ActionResult<List<Pizza>> GetAll() => |
| PizzaService.GetAll(); |
| |
| |
| [HttpGet("{id}")] |
| public ActionResult<Pizza> Get(int id) |
| { |
| var pizza = PizzaService.Get(id); |
| |
| if (pizza == null) |
| return NotFound(); |
| |
| return pizza; |
| } |
| |
| |
| [HttpPost] |
| public IActionResult Create(Pizza pizza) |
| { |
| PizzaService.Add(pizza); |
| return CreatedAtAction(nameof(Get), new { id = pizza.Id }, pizza); |
| } |
| |
| |
| |
| [HttpPut("{id}")] |
| public IActionResult Update(int id, Pizza pizza) |
| { |
| if (id != pizza.Id) |
| return BadRequest(); |
| |
| var existingPizza = PizzaService.Get(id); |
| if (existingPizza is null) |
| return NotFound(); |
| |
| PizzaService.Update(pizza); |
| |
| return NoContent(); |
| } |
| |
| |
| [HttpDelete("{id}")] |
| public IActionResult Delete(int id) |
| { |
| var pizza = PizzaService.Get(id); |
| |
| if (pizza is null) |
| return NotFound(); |
| |
| PizzaService.Delete(id); |
| |
| return NoContent(); |
| } |
| } |
dotnet_learn/Services/PizzaService.cs
| using dotnet_learn.Models; |
| |
| namespace dotnet_learn.Services; |
| |
| public static class PizzaService |
| { |
| static List<Pizza> Pizzas { get; } |
| static int nextId = 3; |
| static PizzaService() |
| { |
| Pizzas = new List<Pizza> |
| { |
| new Pizza { Id = 1, Name = "Classic Italian", IsGlutenFree = false }, |
| new Pizza { Id = 2, Name = "Veggie", IsGlutenFree = true } |
| }; |
| } |
| |
| public static List<Pizza> GetAll() => Pizzas; |
| |
| public static Pizza? Get(int id) => Pizzas.FirstOrDefault(p => p.Id == id); |
| |
| public static void Add(Pizza pizza) |
| { |
| pizza.Id = nextId++; |
| Pizzas.Add(pizza); |
| } |
| |
| public static void Delete(int id) |
| { |
| var pizza = Get(id); |
| if (pizza is null) |
| return; |
| |
| Pizzas.Remove(pizza); |
| } |
| |
| public static void Update(Pizza pizza) |
| { |
| var index = Pizzas.FindIndex(p => p.Id == pizza.Id); |
| if (index == -1) |
| return; |
| |
| Pizzas[index] = pizza; |
| } |
| } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战