{"id":24554014,"url":"https://github.com/fkucukkara/modelvalidationworkshop","last_synced_at":"2025-03-16T14:46:32.307Z","repository":{"id":271101021,"uuid":"908256997","full_name":"fkucukkara/modelValidationWorkshop","owner":"fkucukkara","description":"This document highlights the key differences in model validation between Controller-Based APIs and Minimal APIs, focusing on how ModelState behaves.","archived":false,"fork":false,"pushed_at":"2025-01-12T13:43:26.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-23T02:17:02.176Z","etag":null,"topics":["csharp","model-state","netcore-webapi","validation"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fkucukkara.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-25T15:43:32.000Z","updated_at":"2025-01-12T13:43:29.000Z","dependencies_parsed_at":"2025-01-05T14:44:57.722Z","dependency_job_id":null,"html_url":"https://github.com/fkucukkara/modelValidationWorkshop","commit_stats":null,"previous_names":["fkucukkara/modelvalidationworkshop"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2FmodelValidationWorkshop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2FmodelValidationWorkshop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2FmodelValidationWorkshop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2FmodelValidationWorkshop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fkucukkara","download_url":"https://codeload.github.com/fkucukkara/modelValidationWorkshop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243885891,"owners_count":20363644,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","model-state","netcore-webapi","validation"],"created_at":"2025-01-23T02:17:04.488Z","updated_at":"2025-03-16T14:46:32.272Z","avatar_url":"https://github.com/fkucukkara.png","language":"C#","readme":"# Model Validation: Controller-Based API vs Minimal API\n\nThis document highlights the key differences in **model validation** between Controller-Based APIs and Minimal APIs, focusing on how **ModelState** behaves.\n\n## Sample Model\n```csharp\npublic class Employee\n{\n    [Required(ErrorMessage = \"ID is required.\")]\n    [Range(1, int.MaxValue, ErrorMessage = \"ID must be greater than 0.\")]\n    public int Id { get; set; }\n\n    [Required(ErrorMessage = \"Name is required.\")]\n    public string? Name { get; set; }\n}\n```\n\n## Controller-Based API\nIn a Controller-Based API, **ModelState** is available and validation is automatic when using the `[ApiController]` attribute.\n\n### Example:\n```csharp\n[ApiController]\n[Route(\"api/[controller]\")]\npublic class EmployeeController : ControllerBase\n{\n    [HttpPost]\n    public IActionResult AddEmployee(Employee employee)\n    {\n        if (!ModelState.IsValid)\n        {\n            return BadRequest(ModelState); // Automatically populated.\n        }\n\n        return Ok(employee);\n    }\n}\n```\n\n## Minimal API\nIn Minimal APIs, **ModelState is not available**. Validation must be performed manually using tools like `Validator.TryValidateObject`.\n\n### Example:\n```csharp\nvar builder = WebApplication.CreateBuilder(args);\nvar app = builder.Build();\n\napp.MapPost(\"/employee\", (Employee employee) =\u003e\n{\n    var validationResults = new List\u003cValidationResult\u003e();\n    var validationContext = new ValidationContext(employee);\n\n    if (!Validator.TryValidateObject(employee, validationContext, validationResults, true))\n    {\n        return Results.BadRequest(validationResults);\n    }\n\n    return Results.Ok(employee);\n});\n\napp.Run();\n```\n\n## Comparison\n| Feature                     | Controller-Based API                  | Minimal API                              |\n|-----------------------------|---------------------------------------|-----------------------------------------|\n| **ModelState Behavior**     | Available and automatic validation    | Not available; manual validation needed |\n| **Ease of Use**             | Easier with `[ApiController]`         | Requires custom validation logic         |\n\n## Summary\n- **Controller-Based API**: Leverage automatic **ModelState** validation for simplicity.\n- **Minimal API**: Handle validation manually as **ModelState is not available**.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffkucukkara%2Fmodelvalidationworkshop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffkucukkara%2Fmodelvalidationworkshop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffkucukkara%2Fmodelvalidationworkshop/lists"}