คอลเลกชันของห้องสมุดผู้ช่วยสำหรับโครงการ API ขั้นต่ำ
ห้องสมุดที่ให้ผู้ช่วยเส้นทางสำหรับโครงการ API ขั้นต่ำสำหรับการลงทะเบียนจุดสิ้นสุดอัตโนมัติโดยใช้การสะท้อนกลับ
ห้องสมุดมีให้บริการใน Nuget เพียงค้นหา MinimalHelpers. Routsing ใน แพ็คเกจ Manager GUI หรือเรียกใช้คำสั่งต่อไปนี้ใน . NET CLI :
dotnet add package MinimalHelpers.Routing สร้างชั้นเรียนเพื่อเก็บการลงทะเบียนตัวจัดการเส้นทางของคุณและทำให้การใช้งานอินเทอร์เฟซ IEndpointRouteHandlerBuilder :
public class PeopleEndpoints : MinimalHelpers . Routing . IEndpointRouteHandlerBuilder
{
public static void MapEndpoints ( IEndpointRouteBuilder endpoints )
{
endpoints . MapGet ( "/api/people" , GetList ) ;
endpoints . MapGet ( "/api/people/{id:guid}" , Get ) ;
endpoints . MapPost ( "/api/people" , Insert ) ;
endpoints . MapPut ( "/api/people/{id:guid}" , Update ) ;
endpoints . MapDelete ( "/api/people/{id:guid}" , Delete ) ;
}
// ...
} เรียกใช้ MapEndpoints() วิธีการขยายบนวัตถุ WebApplication ภายใน program.cs ก่อน Run() การเรียกใช้:
// using MinimalHelpers.Routing;
app . MapEndpoints ( ) ;
app . Run ( ) ; โดยค่าเริ่มต้น MapEndpoints() จะสแกนแอสเซมบลีเพื่อค้นหาคลาสที่ใช้อินเทอร์เฟซ IEndpointRouteHandlerBuilder หากตัวจัดการเส้นทางของคุณถูกกำหนดในชุดประกอบอื่นคุณมีสองทางเลือก:
MapEndpoints() โอเวอร์โหลดที่ใช้ชุดประกอบเพื่อสแกนเป็นอาร์กิวเมนต์MapEndpointsFromAssemblyContaining<T>() วิธีการขยายและระบุประเภทที่มีอยู่ในชุดประกอบที่คุณต้องการสแกน นอกจากนี้คุณยังสามารถตัดสินใจได้อย่างชัดเจนว่าประเภทใด (ในกลุ่มที่ใช้อินเทอร์เฟซ IRouteEndpointHandlerBuilder ) ที่คุณต้องการแมปจริง ๆ ผ่านการแสดงผลไปยังวิธีการ MapEndpoints :
app . MapEndpoints ( type =>
{
if ( type . Name . StartsWith ( "Products" ) )
{
return false ;
}
return true ;
} ) ;หมายเหตุ วิธีการเหล่านี้ขึ้นอยู่กับการสะท้อนเพื่อสแกนแอสเซมบลีและค้นหาคลาสที่ใช้อินเทอร์เฟซ
IEndpointRouteHandlerBuilderสิ่งนี้อาจมีผลกระทบด้านประสิทธิภาพโดยเฉพาะอย่างยิ่งในโครงการขนาดใหญ่ หากคุณมีปัญหาด้านประสิทธิภาพให้พิจารณาใช้วิธีการลงทะเบียนที่ชัดเจน ยิ่งไปกว่านั้นโซลูชันนี้ไม่สามารถใช้งานได้กับ AOT ดั้งเดิม
ไลบรารีที่ให้ตัวสร้างแหล่งที่มาสำหรับการลงทะเบียนจุดสิ้นสุดอัตโนมัติในโครงการ API ขั้นต่ำ
ห้องสมุดมีให้บริการใน Nuget เพียงค้นหา MinimalHelpers. Routsing ใน แพ็คเกจ Manager GUI หรือเรียกใช้คำสั่งต่อไปนี้ใน . NET CLI :
dotnet add package MinimalHelpers.Routing.Analyzers สร้างชั้นเรียนเพื่อเก็บการลงทะเบียนตัวจัดการเส้นทางของคุณและทำให้การใช้งานอินเทอร์เฟซ IEndpointRouteHandlerBuilder :
public class PeopleEndpoints : IEndpointRouteHandlerBuilder
{
public static void MapEndpoints ( IEndpointRouteBuilder endpoints )
{
endpoints . MapGet ( "/api/people" , GetList ) ;
endpoints . MapGet ( "/api/people/{id:guid}" , Get ) ;
endpoints . MapPost ( "/api/people" , Insert ) ;
endpoints . MapPut ( "/api/people/{id:guid}" , Update ) ;
endpoints . MapDelete ( "/api/people/{id:guid}" , Delete ) ;
}
// ...
}หมายเหตุ คุณจะต้องใช้แพ็คเกจ minimalHelpers.routing.analyzers เท่านั้น ด้วยเครื่องกำเนิดแหล่งที่มานี้อินเตอร์เฟส
IEndpointRouteHandlerBuilderนั้นสร้างขึ้นอัตโนมัติ
เรียกใช้ MapEndpoints() วิธีการขยายบนวัตถุ WebApplication ภายใน program.cs ก่อน Run() การเรียกใช้:
app . MapEndpoints ( ) ;
app . Run ( ) ;หมายเหตุ เมธอด
MapEndpointsถูกสร้างขึ้นโดยตัวสร้างแหล่งที่มา
ห้องสมุดที่ให้ผู้ช่วย OpenAPI สำหรับโครงการ API ขั้นต่ำ
ห้องสมุดมีให้บริการใน Nuget เพียงค้นหา MinimalHelpers.openapi ใน แพ็คเกจ Manager GUI หรือเรียกใช้คำสั่งต่อไปนี้ใน . NET CLI :
dotnet add package MinimalHelpers.OpenApiวิธีการขยายสำหรับ openapi
ไลบรารีนี้มีวิธีการขยายบางอย่างที่ทำให้การกำหนดค่า OpenAPI ง่ายขึ้นในโครงการ API ขั้นต่ำ ตัวอย่างเช่นมีความเป็นไปได้ที่จะปรับแต่งคำอธิบายของการตอบกลับโดยใช้รหัสสถานะ:
endpoints . MapPost ( "login" , LoginAsync )
. AllowAnonymous ( )
. WithValidation < LoginRequest > ( )
. Produces < LoginResponse > ( StatusCodes . Status200OK )
. Produces < LoginResponse > ( StatusCodes . Status206PartialContent )
. Produces ( StatusCodes . Status403Forbidden )
. ProducesValidationProblem ( )
. WithOpenApi ( operation =>
{
operation . Summary = "Performs the login of a user" ;
operation . Response ( StatusCodes . Status200OK ) . Description = "Login successful" ;
operation . Response ( StatusCodes . Status206PartialContent ) . Description = "The user is logged in, but the password has expired and must be changed" ;
operation . Response ( StatusCodes . Status400BadRequest ) . Description = "Incorrect username and/or password" ;
operation . Response ( StatusCodes . Status403Forbidden ) . Description = "The user was blocked due to too many failed logins" ;
return operation ;
} ) ;วิธีการขยายสำหรับ RoutHandlerBuilder
บ่อยครั้งที่เรามีจุดสิ้นสุดที่มีค่าส่งคืน 4xx หลายค่าซึ่งแต่ละค่าจะสร้างการตอบสนอง ProblemDetails :
endpoints . MapGet ( "/api/people/{id:guid}" , Get )
. ProducesProblem ( StatusCodes . Status400BadRequest )
. ProducesProblem ( StatusCodes . Status401Unauthorized )
. ProducesProblem ( StatusCodes . Status403Forbidden )
. ProducesProblem ( StatusCodes . Status404NotFound ) ; เพื่อหลีกเลี่ยงการโทรหลายครั้งเพื่อ ProducesProblem เราสามารถใช้วิธีการขยาย ProducesDefaultProblem
endpoints . MapGet ( "/api/people/{id:guid}" , Get )
. ProducesDefaultProblem ( StatusCodes . Status400BadRequest , StatusCodes . Status401Unauthorized ,
StatusCodes . Status403Forbidden , StatusCodes . Status404NotFound ) ; ห้องสมุดที่ให้ตัวกรองปลายทางสำหรับโครงการ API ขั้นต่ำเพื่อทำการตรวจสอบความถูกต้องด้วยคำอธิบายประกอบข้อมูลโดยใช้ไลบรารี minivalidation
ห้องสมุดมีให้บริการใน Nuget เพียงค้นหา MinimalHelpers.Validation ใน แพ็คเกจ Manager GUI หรือเรียกใช้คำสั่งต่อไปนี้ใน . NET CLI :
dotnet add package MinimalHelpers.Validationตกแต่งชั้นเรียนด้วยคุณสมบัติเพื่อกำหนดกฎการตรวจสอบความถูกต้อง:
using System . ComponentModel . DataAnnotations ;
public class Person
{
[ Required ]
[ MaxLength ( 20 ) ]
public string ? FirstName { get ; set ; }
[ Required ]
[ MaxLength ( 20 ) ]
public string ? LastName { get ; set ; }
[ MaxLength ( 50 ) ]
public string ? City { get ; set ; }
} เพิ่มวิธีการขยาย WithValidation<T>() เพื่อเปิดใช้งานตัวกรองการตรวจสอบความถูกต้อง:
using MinimalHelpers . Validation ;
app . MapPost ( "/api/people" , ( Person person ) =>
{
// ...
} )
. WithValidation < Person > ( ) ; หากการตรวจสอบความถูกต้องล้มเหลวการตอบสนองจะเป็น 400 Bad Request ด้วยวัตถุ ValidationProblemDetails ที่มีข้อผิดพลาดการตรวจสอบความถูกต้องตัวอย่างเช่น:
{
"type" : " https://tools.ietf.org/html/rfc9110#section-15.5.1 " ,
"title" : " One or more validation errors occurred " ,
"status" : 400 ,
"instance" : " /api/people " ,
"traceId" : " 00-009c0162ba678cae2ee391815dbbb59d-0a3a5b0c16d053e6-00 " ,
"errors" : {
"FirstName" : [
" The field FirstName must be a string or array type with a maximum length of '20'. "
],
"LastName" : [
" The LastName field is required. "
]
}
} หากคุณต้องการปรับแต่งการตรวจสอบคุณสามารถใช้วิธีการขยาย ConfigureValidation :
using MinimalHelpers . Validation ;
builder . Services . ConfigureValidation ( options =>
{
// If you want to get errors as a list instead of a dictionary.
options . ErrorResponseFormat = ErrorResponseFormat . List ;
// The default is "One or more validation errors occurred"
options . ValidationErrorTitleMessageFactory =
( context , errors ) => $ "There was { errors . Values . Sum ( v => v . Length ) } error(s)" ;
} ) ; คุณสามารถใช้ ValidationErrorTitleMessageFactory ได้เช่นหากคุณต้องการแปลคุณสมบัติ title ของการตอบกลับโดยใช้ไฟล์ RESX
ห้องสมุดที่ให้ตัวกรองปลายทางสำหรับโครงการ API ขั้นต่ำเพื่อดำเนินการตรวจสอบความถูกต้องโดยใช้ FluentValidation
ห้องสมุดมีให้บริการใน Nuget เพียงค้นหา MinimalHelpers.FluentValidation ใน แพ็คเกจ Manager GUI หรือเรียกใช้คำสั่งต่อไปนี้ใน . NET CLI :
dotnet add package MinimalHelpers.FluentValidationสร้างคลาสที่ขยาย AbstractValidator และกำหนดกฎการตรวจสอบความถูกต้อง:
using FluentValidation ;
public record class Product ( string Name , string Description , double UnitPrice ) ;
public class ProductValidator : AbstractValidator < Product >
{
public ProductValidator ( )
{
RuleFor ( p => p . Name ) . NotEmpty ( ) . MaximumLength ( 50 ) . EmailAddress ( ) ;
RuleFor ( p => p . Description ) . MaximumLength ( 500 ) ;
RuleFor ( p => p . UnitPrice ) . GreaterThan ( 0 ) ;
}
}ลงทะเบียนผู้ตรวจสอบในคอลเลกชันบริการ:
using FluentValidation ;
// Assuming the validators are in the same assembly as the Program class
builder . Services . AddValidatorsFromAssemblyContaining < Program > ( ) ; เพิ่มวิธีการขยาย WithValidation<T>() เพื่อเปิดใช้งานตัวกรองการตรวจสอบความถูกต้อง:
using MinimalHelpers . FluentValidation ;
app . MapPost ( "/api/products" , ( Product product ) =>
{
// ...
} )
. WithValidation < Product > ( ) ; หากการตรวจสอบความถูกต้องล้มเหลวการตอบสนองจะเป็น 400 Bad Request ด้วยวัตถุ ValidationProblemDetails ที่มีข้อผิดพลาดการตรวจสอบความถูกต้องตัวอย่างเช่น:
{
"type" : " https://tools.ietf.org/html/rfc9110#section-15.5.1 " ,
"title" : " One or more validation errors occurred " ,
"status" : 400 ,
"instance" : " /api/products " ,
"traceId" : " 00-f4ced0ae470424dd04cbcebe5f232dc5-bbdcc59f310ebfb8-00 " ,
"errors" : {
"Name" : [
" 'Name' cannot be empty. "
],
"UnitPrice" : [
" 'Unit Price' must be grater than '0'. "
]
}
} หากคุณต้องการปรับแต่งการตรวจสอบคุณสามารถใช้วิธีการขยาย ConfigureValidation :
using MinimalHelpers . Validation ;
builder . Services . ConfigureValidation ( options =>
{
// If you want to get errors as a list instead of a dictionary.
options . ErrorResponseFormat = ErrorResponseFormat . List ;
// The default is "One or more validation errors occurred"
options . ValidationErrorTitleMessageFactory =
( context , errors ) => $ "There was { errors . Values . Sum ( v => v . Length ) } error(s)" ;
} ) ; คุณสามารถใช้ ValidationErrorTitleMessageFactory ได้เช่นหากคุณต้องการแปลคุณสมบัติ title ของการตอบกลับโดยใช้ไฟล์ RESX
มีส่วนช่วย
โครงการมีการพัฒนาอย่างต่อเนื่อง ยินดีต้อนรับการมีส่วนร่วม อย่าลังเลที่จะยื่นปัญหาและดึงคำขอบน repo และเราจะจัดการกับพวกเขาเท่าที่จะทำได้