2022-09-10 05:11:17 +08:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace ProblemDetailsWebApi.Controllers;
|
|
|
|
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
[ApiController]
|
|
|
|
public class ValuesController : ControllerBase
|
|
|
|
{
|
|
|
|
// /api/values/Divide/1/2
|
|
|
|
[HttpGet("{Numerator}/{Denominator}")]
|
|
|
|
public IActionResult Divide(double Numerator, double Denominator)
|
|
|
|
{
|
|
|
|
if (Denominator == 0)
|
|
|
|
{
|
2022-09-20 10:27:45 +08:00
|
|
|
var errorType = new MathErrorFeature { MathError =
|
|
|
|
MathErrorType.DivisionByZeroError };
|
2022-09-15 10:06:37 +08:00
|
|
|
HttpContext.Features.Set(errorType);
|
2022-09-10 05:11:17 +08:00
|
|
|
return BadRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
var calculation = Numerator / Denominator;
|
|
|
|
return Ok(calculation);
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:07:59 +08:00
|
|
|
// /api/values/Squareroot/4
|
2022-09-10 05:11:17 +08:00
|
|
|
[HttpGet("{radicand}")]
|
|
|
|
public IActionResult Squareroot(double radicand)
|
|
|
|
{
|
|
|
|
if (radicand < 0)
|
|
|
|
{
|
2022-09-20 10:27:45 +08:00
|
|
|
var errorType = new MathErrorFeature { MathError =
|
|
|
|
MathErrorType.NegativeRadicandError };
|
2022-09-15 10:06:37 +08:00
|
|
|
HttpContext.Features.Set(errorType);
|
2022-09-10 05:11:17 +08:00
|
|
|
return BadRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
var calculation = Math.Sqrt(radicand);
|
|
|
|
return Ok(calculation);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|