2022-09-21 08:54:56 +08:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace ProblemDetailsWebApi.Controllers;
|
2022-09-23 10:12:01 +08:00
|
|
|
// <snippet_1>
|
2022-09-21 08:54:56 +08:00
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
[ApiController]
|
|
|
|
public class Values2Controller : ControllerBase
|
|
|
|
{
|
|
|
|
// /api/values2/divide/1/2
|
|
|
|
[HttpGet("{Numerator}/{Denominator}")]
|
|
|
|
public IActionResult Divide(double Numerator, double Denominator)
|
|
|
|
{
|
|
|
|
if (Denominator == 0)
|
|
|
|
{
|
|
|
|
return BadRequest();
|
|
|
|
}
|
|
|
|
|
2022-09-28 06:05:38 +08:00
|
|
|
return Ok(Numerator / Denominator);
|
2022-09-21 08:54:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// /api/values2 /squareroot/4
|
|
|
|
[HttpGet("{radicand}")]
|
|
|
|
public IActionResult Squareroot(double radicand)
|
|
|
|
{
|
|
|
|
if (radicand < 0)
|
|
|
|
{
|
|
|
|
return BadRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(Math.Sqrt(radicand));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// </snippet_1>
|
2022-09-10 05:11:17 +08:00
|
|
|
|
2022-09-21 10:09:47 +08:00
|
|
|
// <snippet>
|
2022-09-10 05:11:17 +08:00
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
[ApiController]
|
|
|
|
public class ValuesController : ControllerBase
|
|
|
|
{
|
2022-09-21 08:54:56 +08:00
|
|
|
// /api/values/divide/1/2
|
2022-09-10 05:11:17 +08:00
|
|
|
[HttpGet("{Numerator}/{Denominator}")]
|
|
|
|
public IActionResult Divide(double Numerator, double Denominator)
|
|
|
|
{
|
|
|
|
if (Denominator == 0)
|
|
|
|
{
|
2022-09-21 08:54:56 +08:00
|
|
|
var errorType = new MathErrorFeature
|
|
|
|
{
|
2022-09-22 05:50:56 +08:00
|
|
|
MathError = MathErrorType.DivisionByZeroError
|
2022-09-21 08:54:56 +08:00
|
|
|
};
|
2022-09-15 10:06:37 +08:00
|
|
|
HttpContext.Features.Set(errorType);
|
2022-09-10 05:11:17 +08:00
|
|
|
return BadRequest();
|
|
|
|
}
|
|
|
|
|
2022-09-28 06:05:38 +08:00
|
|
|
return Ok(Numerator / Denominator);
|
2022-09-10 05:11:17 +08:00
|
|
|
}
|
|
|
|
|
2022-09-21 08:54:56 +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-21 08:54:56 +08:00
|
|
|
var errorType = new MathErrorFeature
|
|
|
|
{
|
2022-09-22 05:50:56 +08:00
|
|
|
MathError = MathErrorType.NegativeRadicandError
|
2022-09-21 08:54:56 +08:00
|
|
|
};
|
2022-09-15 10:06:37 +08:00
|
|
|
HttpContext.Features.Set(errorType);
|
2022-09-10 05:11:17 +08:00
|
|
|
return BadRequest();
|
|
|
|
}
|
|
|
|
|
2022-09-27 10:59:19 +08:00
|
|
|
return Ok(Math.Sqrt(radicand));
|
2022-09-10 05:11:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-09-21 08:54:56 +08:00
|
|
|
// </snippet>
|
2022-09-22 05:32:33 +08:00
|
|
|
|
2022-09-22 05:50:56 +08:00
|
|
|
// <snippet3>
|
2022-09-22 05:32:33 +08:00
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
[ApiController]
|
|
|
|
public class Values3Controller : ControllerBase
|
|
|
|
{
|
2022-09-22 05:50:56 +08:00
|
|
|
// /api/values3/divide/1/2
|
2022-09-22 05:32:33 +08:00
|
|
|
[HttpGet("{Numerator}/{Denominator}")]
|
|
|
|
public IActionResult Divide(double Numerator, double Denominator)
|
|
|
|
{
|
|
|
|
if (Denominator == 0)
|
|
|
|
{
|
|
|
|
var errorType = new MathErrorFeature
|
|
|
|
{
|
2022-09-22 05:50:56 +08:00
|
|
|
MathError = MathErrorType.DivisionByZeroError
|
2022-09-22 05:32:33 +08:00
|
|
|
};
|
|
|
|
HttpContext.Features.Set(errorType);
|
|
|
|
return Problem(
|
2022-09-22 05:50:56 +08:00
|
|
|
title: "Bad Input",
|
2022-09-22 06:18:19 +08:00
|
|
|
detail: "Divison by zero is not defined.",
|
2022-09-22 05:32:33 +08:00
|
|
|
type: "https://en.wikipedia.org/wiki/Division_by_zero",
|
|
|
|
statusCode: StatusCodes.Status400BadRequest
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-28 06:05:38 +08:00
|
|
|
return Ok(Numerator / Denominator);
|
2022-09-22 05:32:33 +08:00
|
|
|
}
|
|
|
|
|
2022-09-22 05:50:56 +08:00
|
|
|
// /api/values3/squareroot/4
|
2022-09-22 05:32:33 +08:00
|
|
|
[HttpGet("{radicand}")]
|
|
|
|
public IActionResult Squareroot(double radicand)
|
|
|
|
{
|
|
|
|
if (radicand < 0)
|
|
|
|
{
|
|
|
|
var errorType = new MathErrorFeature
|
|
|
|
{
|
2022-09-22 05:50:56 +08:00
|
|
|
MathError = MathErrorType.NegativeRadicandError
|
2022-09-22 05:32:33 +08:00
|
|
|
};
|
|
|
|
HttpContext.Features.Set(errorType);
|
|
|
|
return Problem(
|
2022-09-22 05:50:56 +08:00
|
|
|
title: "Bad Input",
|
2022-09-22 06:18:19 +08:00
|
|
|
detail: "Negative or complex numbers are not valid input.",
|
2022-09-22 05:32:33 +08:00
|
|
|
type: "https://en.wikipedia.org/wiki/Square_root",
|
|
|
|
statusCode: StatusCodes.Status400BadRequest
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-27 10:59:19 +08:00
|
|
|
return Ok(Math.Sqrt(radicand));
|
2022-09-22 05:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-09-22 05:50:56 +08:00
|
|
|
// </snippet3>
|
2022-09-27 10:59:19 +08:00
|
|
|
|
|
|
|
// To test unhandled exceptions
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
[ApiController]
|
|
|
|
public class Values4Controller : ControllerBase
|
|
|
|
{
|
2022-09-28 06:05:38 +08:00
|
|
|
// /api/values4/divide/1/2
|
2022-09-27 10:59:19 +08:00
|
|
|
[HttpGet("{Numerator}/{Denominator}")]
|
|
|
|
public IActionResult Divide(double Numerator, double Denominator)
|
|
|
|
{
|
|
|
|
return Ok(Numerator/Denominator);
|
|
|
|
}
|
|
|
|
|
|
|
|
// /api/values4/squareroot/4
|
|
|
|
[HttpGet("{radicand}")]
|
|
|
|
public IActionResult Squareroot(double radicand)
|
|
|
|
{
|
2022-09-28 06:05:38 +08:00
|
|
|
return Ok(Math.Sqrt(radicand));
|
2022-09-27 10:59:19 +08:00
|
|
|
}
|
|
|
|
}
|