Skip to content

Instantly share code, notes, and snippets.

@IntegerMan
Created December 16, 2019 01:01
Embed
What would you like to do?
[HttpGet]
[Route("add/{x}/{y}")]
public ObjectResult Add(string x, string y)
{
try
{
ValidateInputs(x, y);
var xVal = decimal.Parse(x);
var yVal = decimal.Parse(y);
return Ok(xVal + yVal);
}
catch (ValidationException ex)
{
return BadRequest(ex.Message);
}
}
private void ValidateInputs(string x, string y)
{
if (!decimal.TryParse(x, out _))
{
throw new ValidationException($"Cannot parse '{x}' to a number");
}
if (!decimal.TryParse(y, out _))
{
throw new ValidationException($"Cannot parse '{y}' to a number");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment