Route constraints for validation of Polish NIP, PESEL, REGON for .NET 5
RouteConstraints can be installed using the Nuget package manager or the dotnet CLI.
Install-Package Sulmar.AspNetCore.Routing.RouteConstraints
dotnet add package Sulmar.AspNetCore.Routing.RouteConstraints
- PESEL
public void ConfigureServices(IServiceCollection services)
{
services.AddPeselConstraint();
}
- REGON
public void ConfigureServices(IServiceCollection services)
{
services.AddRegonConstraint();
}
- NIP
public void ConfigureServices(IServiceCollection services)
{
services.AddNipConstraint();
}
- or all together
public void ConfigureServices(IServiceCollection services)
{
services.AddPolishConstraints();
}
- PESEL
// GET api/customers/{number}
[HttpGet("{number:pesel}")]
public ActionResult<Customer> GetByPesel(string number)
{
var customer = customerService.GetByPesel(number);
if (customer == null)
return NotFound();
return Ok(customer);
}
- REGON
// GET api/customers/{number}
[HttpGet("{number:regon}")]
public ActionResult<Customer> GetByRegon(string number)
{
var customer = customerService.GetByRegon(number);
if (customer == null)
return NotFound();
return Ok(customer);
}
- NIP
// GET api/customers/{number}
[HttpGet("{number:nip}")]
public ActionResult<Customer> GetByNip(string number)
{
var customer = customerService.GetByNip(number);
if (customer == null)
return NotFound();
return Ok(customer);
}