Manually validate Model in ASP.NET Core Web API (edit)
- form action="#" method="post" enctype="multipart/form-data" ...
- input type="text" ...
- input type="file"
- input type="checkbox" ...
- input type="radio" ...
- input type="submit" ...
- Backend: Build Model from Request.Forms
- Backend: Validation using DataAnnotation
Validation with the Data Annotation Validators (C#)
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; using System.Net;
namespace ShopApi.Controllers
{
[ApiVersion("1.0")]
[ApiVersion("2.0")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
public class StoreMemberController : BaseController
{
...
[ApiKey]
[AllowAnonymous]
[HttpPost("register"), DisableRequestSizeLimit]
public async Task<IActionResult> Register(CancellationToken cancellationToken)
{
try
{
var request = new StoreMemberRegisterCommand();
var hasFileUploaded = Request.Form.Files.Count > 0;
IFormFile file = hasFileUploaded ? Request.Form.Files[0] : null;
var formData = Request.Form.ToDictionary(x => x.Key, x => x.Value.ToString());
request.StoreName = formData[nameof(StoreRegisterDto.store_name)];
...
var context = new ValidationContext(request, serviceProvider: null, items: null); var results = new List<ValidationResult>(); var isValid = Validator.TryValidateObject(request, context, results); if (!isValid) { var errors = (from x in results select x.ErrorMessage).ToArray(); return Ok(new { Success = false, Status = HttpStatusCode.BadRequest, StatusCode = (int)HttpStatusCode.BadRequest, Data = errors }); }
...
}
...
}
}
}
References
Super Basic HTTP API with ASP.NET Core
How To Build A Basic HTTP API With ASP.NET Core | Khalid Abuhakmeh (HAY HAY HAY HAY HAY)
Build a REST API for your Mobile Apps with ASP.NET Core
Build a REST API for your Mobile Apps with ASP.NET Core - Stormpath User Identity API (HAY HAY HAY HAY HAY)
c# - .NET Core Web API model validation - Stack Overflow (HAY HAY HAY)
Manual Validation with Data Annotations (odetocode.com) (HAY HAY HAY)
Combining ASP.NET Core validation attributes with Value Objects · Enterprise Craftsmanship
Handling validation responses for ASP.NET Core Web API | Jerrie Pelser's Blog
ASP.NET Core REST API Model Validation (pragimtech.com)
javascript - How to append whole set of model to formdata and obtain it in MVC - Stack Overflow (HAY HAY HAY)
brunomoita/InventoryService: ASP.NET Inventory API (github.com)
4 Ways To Get Form Data In ASP.NET MVC 5
4 Ways to Get Form Data in ASP.NET MVC 5 (completecsharptutorial.com) (HAY HAY HAY HAY HAY)
Angular + ASP.NET Core + Upload Files
Uploading Files With ASP.NET Core and Angular
Uploading Files With ASP.NET Core and Angular | dotnetthoughts
ASP.NET Core 6.0
ASP.NET Core 6.0 - Minimal API Example - Todo API implementation using Minimal API, Entity Framework Core In Memory Provider and Open API
Integration Testing ASP.NET Core 6 Minimal APIs - An example of integration testing ASP.NET Core 6 Minimal hosting and actions
Todo REST API with ASP.NET Core Minimal APIs
davidfowl/CommunityStandUpMinimalAPI (github.com)
ASP.NET 5.0 on AWS
Câu hỏi thường gặp về Amazon S3: Amazon Simple Storage Service (S3) — - — AWS
Câu hỏi thường gặp về Amazon EC2: Câu hỏi thường gặp về Amazon EC2 – Amazon Web Services
.NET 5 on AWS | AWS Developer Tools Blog (amazon.com)
Exploring .NET 5 with the AWS Toolkit for Visual Studio | AWS Developer Tools Blog (amazon.com)
+ AWS SDK for .NET: AWS SDK for .NET (amazon.com)
+ AWS Toolkit for Visual Studio: AWS Toolkit for Visual Studio (amazon.com)
+ Amazon DynamoDB: Amazon DynamoDB | NoSQL Key-Value Database | Amazon Web Services
+ AWS CodePipeline: AWS CodePipeline | Continuous Integration & Continuous Delivery (amazon.com)
Integration Testing ASP.NET Core 6 Minimal APIs