我有一个必需的属性(使用 DataAnnotations),它默认所有不带 ? 的值。在类型后标记。默认为 01/01/0001。如何将其设为空白或默认为某个日期?
PS 我还有两个 int 属性,它们默认为 0。这没有意义,因为我不知道它是如何默认的。
如何将“/api/employees”路由移出program.cs?
程序.cs:
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("AppDb");
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddDbContext<AppDbContext>(o => o.UseSqlServer(connectionString));
builder.Services.AddAuthorization();
builder.Services.AddAuthentication();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (builder.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseAuthentication();
app.MapControllers();
app.MapGet("/", () => "Hello World!");
app.Run();
Run Code Online (Sandbox Code Playgroud)
我见过微软的人做了这样的事情:
public class EmployeeApi
{
public static void MapRoutes(IEndpointRouteBuilder routes)
{
routes.MapGet("/api/employees", async ([FromServices] AppDbContext db) =>
{
return await db.Employees.ToListAsync();
});
routes.MapGet("api/employees/{id}", async (int id, [FromServices] AppDbContext db) =>
{
return await …Run Code Online (Sandbox Code Playgroud)