* Add controller to middleware example

* fix code
pull/62/head
Rick Anderson 2022-09-20 14:54:56 -10:00 committed by GitHub
parent 3d61ef0900
commit d93184b0bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 7 deletions

View File

@ -1,3 +1,42 @@
#define FIRST // FIRST IMPROVED
#if NEVER
#elif FIRST
// <snippet_1>
using Microsoft.AspNetCore.Mvc;
namespace ProblemDetailsWebApi.Controllers;
[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>
#elif IMPROVED
// <snippet>
using Microsoft.AspNetCore.Mvc;
namespace ProblemDetailsWebApi.Controllers;
@ -6,14 +45,17 @@ namespace ProblemDetailsWebApi.Controllers;
[ApiController]
public class ValuesController : ControllerBase
{
// /api/values/Divide/1/2
// /api/values/divide/1/2
[HttpGet("{Numerator}/{Denominator}")]
public IActionResult Divide(double Numerator, double Denominator)
{
if (Denominator == 0)
{
var errorType = new MathErrorFeature { MathError =
MathErrorType.DivisionByZeroError };
var errorType = new MathErrorFeature
{
MathError =
MathErrorType.DivisionByZeroError
};
HttpContext.Features.Set(errorType);
return BadRequest();
}
@ -22,14 +64,17 @@ public class ValuesController : ControllerBase
return Ok(calculation);
}
// /api/values/Squareroot/4
// /api/values/squareroot/4
[HttpGet("{radicand}")]
public IActionResult Squareroot(double radicand)
{
if (radicand < 0)
{
var errorType = new MathErrorFeature { MathError =
MathErrorType.NegativeRadicandError };
var errorType = new MathErrorFeature
{
MathError =
MathErrorType.NegativeRadicandError
};
HttpContext.Features.Set(errorType);
return BadRequest();
}
@ -39,3 +84,5 @@ public class ValuesController : ControllerBase
}
}
// </snippet>
#endif

View File

@ -91,7 +91,6 @@ if (app.Environment.IsDevelopment())
}
app.UseHttpsRedirection();
app.UseStatusCodePages();
// Middleware to handle writing problem details to the response.