AspNetCore.Docs.Samples/fundamentals/middleware/problem-details-service/Controllers/ValuesController.cs

42 lines
1.2 KiB
C#
Raw Normal View History

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 };
HttpContext.Features.Set(errorType);
return BadRequest();
}
var calculation = Numerator / Denominator;
return Ok(calculation);
}
// /api/values/Squareroot/4
[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 };
HttpContext.Features.Set(errorType);
return BadRequest();
}
var calculation = Math.Sqrt(radicand);
return Ok(calculation);
}
}