Skip to content

Instantly share code, notes, and snippets.

@IntegerMan
Created December 16, 2019 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IntegerMan/1f5bfe536ddfa79d5823c7e615f723af to your computer and use it in GitHub Desktop.
Save IntegerMan/1f5bfe536ddfa79d5823c7e615f723af to your computer and use it in GitHub Desktop.
[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