Aspnet Core

作者 mindrally97184105b5da无许可证269 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Guidelines for ASP.NET Core web development covering API design, authentication, caching, and best practices

AI 生成的概览

ASP.NET Core Web 开发指南,涵盖 API 设计、身份验证、缓存和最佳实践。

功能
该技能为构建 ASP.NET Core Web 应用和 API 提供参考指导。内容涵盖项目结构、RESTful 控制器设计、中间件顺序、缓存策略、JWT 身份验证与基于策略的授权、错误处理、验证、配置以及 Swagger/OpenAPI 文档。它产出的是代码示例和约定,而非可执行产物。
适用场景
适用于设计或实现 ASP.NET Core API 和 Web 服务时。适合解答有关 ASP.NET Core 中控制器、中间件、缓存、身份验证、授权、验证或 API 文档的问题。
运行要求
不包含脚本或工具,仅为说明性指导。应用其中的示例需要 ASP.NET Core 项目和 C# 基础,可选包包括 FluentValidation、Redis 分布式缓存和 Swagger/OpenAPI。

ASP.NET Core Development Guidelines

You are an expert in ASP.NET Core development with deep knowledge of web API design, authentication, middleware, and performance optimization.

Project Structure

src/  Controllers/      # API endpoints  Models/          # Domain models and DTOs  Services/        # Business logic  Data/            # DbContext and repositories  Middleware/      # Custom middleware  Extensions/      # Extension methods  Configuration/   # App configurationProgram.csappsettings.json

Controller Design

RESTful Controllers

csharp
[ApiController][Route("api/[controller]")]public class ProductsController : ControllerBase{    private readonly IProductService _productService;
    public ProductsController(IProductService productService)    {        _productService = productService;    }
    [HttpGet]    public async Task<ActionResult<IEnumerable<ProductDto>>> GetProducts()    {        var products = await _productService.GetAllAsync();        return Ok(products);    }
    [HttpGet("{id}")]    public async Task<ActionResult<ProductDto>> GetProduct(int id)    {        var product = await _productService.GetByIdAsync(id);        if (product == null)            return NotFound();        return Ok(product);    }
    [HttpPost]    public async Task<ActionResult<ProductDto>> CreateProduct(CreateProductDto dto)    {        var product = await _productService.CreateAsync(dto);        return CreatedAtAction(nameof(GetProduct), new { id = product.Id }, product);    }}

Best Practices

  • Keep controllers thin
  • Use DTOs for request/response
  • Return appropriate status codes
  • Use async/await consistently
  • Implement proper validation

Middleware

Custom Middleware

csharp
public class RequestLoggingMiddleware{    private readonly RequestDelegate _next;    private readonly ILogger<RequestLoggingMiddleware> _logger;
    public RequestLoggingMiddleware(RequestDelegate next, ILogger<RequestLoggingMiddleware> logger)    {        _next = next;        _logger = logger;    }
    public async Task InvokeAsync(HttpContext context)    {        _logger.LogInformation("Request: {Method} {Path}", context.Request.Method, context.Request.Path);        await _next(context);        _logger.LogInformation("Response: {StatusCode}", context.Response.StatusCode);    }}

Middleware Order

  1. Exception handling
  2. HTTPS redirection
  3. Static files
  4. Routing
  5. CORS
  6. Authentication
  7. Authorization
  8. Custom middleware
  9. Endpoints

Caching

Response Caching

csharp
[HttpGet][ResponseCache(Duration = 60)]public async Task<ActionResult<IEnumerable<ProductDto>>> GetProducts()

Memory Caching

csharp
public class ProductService : IProductService{    private readonly IMemoryCache _cache;
    public async Task<Product?> GetByIdAsync(int id)    {        return await _cache.GetOrCreateAsync($"product_{id}", async entry =>        {            entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10);            return await _repository.GetByIdAsync(id);        });    }}

Distributed Caching

  • Use Redis for multi-server scenarios
  • Configure in Program.cs
  • Use IDistributedCache interface

Authentication

JWT Configuration

csharp
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)    .AddJwtBearer(options =>    {        options.TokenValidationParameters = new TokenValidationParameters        {            ValidateIssuer = true,            ValidateAudience = true,            ValidateLifetime = true,            ValidateIssuerSigningKey = true,            ValidIssuer = builder.Configuration["Jwt:Issuer"],            ValidAudience = builder.Configuration["Jwt:Audience"],            IssuerSigningKey = new SymmetricSecurityKey(                Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!))        };    });

Token Generation

csharp
public string GenerateToken(User user){    var claims = new[]    {        new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),        new Claim(ClaimTypes.Email, user.Email),        new Claim(ClaimTypes.Role, user.Role)    };
    var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]!));    var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
    var token = new JwtSecurityToken(        issuer: _config["Jwt:Issuer"],        audience: _config["Jwt:Audience"],        claims: claims,        expires: DateTime.UtcNow.AddHours(1),        signingCredentials: creds);
    return new JwtSecurityTokenHandler().WriteToken(token);}

Authorization

Policy-Based Authorization

csharp
builder.Services.AddAuthorization(options =>{    options.AddPolicy("AdminOnly", policy => policy.RequireRole("Admin"));    options.AddPolicy("PremiumUser", policy => policy.RequireClaim("Subscription", "Premium"));});
[Authorize(Policy = "AdminOnly")][HttpDelete("{id}")]public async Task<IActionResult> DeleteProduct(int id)

Error Handling

Global Exception Handler

csharp
app.UseExceptionHandler(errorApp =>{    errorApp.Run(async context =>    {        context.Response.StatusCode = 500;        context.Response.ContentType = "application/json";
        var error = context.Features.Get<IExceptionHandlerFeature>();        if (error != null)        {            await context.Response.WriteAsJsonAsync(new            {                error = "An error occurred",                detail = app.Environment.IsDevelopment() ? error.Error.Message : null            });        }    });});

Validation

FluentValidation

csharp
public class CreateProductValidator : AbstractValidator<CreateProductDto>{    public CreateProductValidator()    {        RuleFor(x => x.Name).NotEmpty().MaximumLength(100);        RuleFor(x => x.Price).GreaterThan(0);        RuleFor(x => x.Category).NotEmpty();    }}

Configuration

Options Pattern

csharp
builder.Services.Configure<SmtpSettings>(builder.Configuration.GetSection("Smtp"));
public class EmailService{    private readonly SmtpSettings _settings;
    public EmailService(IOptions<SmtpSettings> options)    {        _settings = options.Value;    }}

Documentation

Swagger/OpenAPI

csharp
builder.Services.AddSwaggerGen(c =>{    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });    c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme    {        Type = SecuritySchemeType.Http,        Scheme = "bearer",        BearerFormat = "JWT"    });});

来源与署名

来源:mindrally/skills位于aspnet-core提交9718410

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架