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

148 lines
3.7 KiB
C#
Raw Permalink Normal View History

using Microsoft.AspNetCore.Mvc;
namespace ProblemDetailsWebApi.Controllers;
2022-09-23 10:12:01 +08:00
// <snippet_1>
[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();
}
return Ok(Numerator / Denominator);
}
// /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-21 10:09:47 +08:00
// <snippet>
[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)
{
var errorType = new MathErrorFeature
{
2022-09-22 05:50:56 +08:00
MathError = MathErrorType.DivisionByZeroError
};
HttpContext.Features.Set(errorType);
return BadRequest();
}
return Ok(Numerator / Denominator);
}
// /api/values/squareroot/4
[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
};
HttpContext.Features.Set(errorType);
return BadRequest();
}
2022-09-27 10:59:19 +08:00
return Ok(Math.Sqrt(radicand));
}
}
// </snippet>
2022-09-22 05:50:56 +08:00
// <snippet3>
[Route("api/[controller]/[action]")]
[ApiController]
public class Values3Controller : ControllerBase
{
2022-09-22 05:50:56 +08:00
// /api/values3/divide/1/2
[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
};
HttpContext.Features.Set(errorType);
return Problem(
2022-09-22 05:50:56 +08:00
title: "Bad Input",
detail: "Divison by zero is not defined.",
type: "https://en.wikipedia.org/wiki/Division_by_zero",
statusCode: StatusCodes.Status400BadRequest
);
}
return Ok(Numerator / Denominator);
}
2022-09-22 05:50:56 +08:00
// /api/values3/squareroot/4
[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
};
HttpContext.Features.Set(errorType);
return Problem(
2022-09-22 05:50:56 +08:00
title: "Bad Input",
detail: "Negative or complex numbers are not valid input.",
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: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
{
// /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)
{
return Ok(Math.Sqrt(radicand));
2022-09-27 10:59:19 +08:00
}
}