From d93184b0bbf36ba5c3ed1e6733940eaf414d1023 Mon Sep 17 00:00:00 2001
From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>
Date: Tue, 20 Sep 2022 14:54:56 -1000
Subject: [PATCH] Pd9 (#61)
* Add controller to middleware example
* fix code
---
.../Controllers/ValuesController.cs | 59 +++++++++++++++++--
.../problem-details-service/Program.cs | 1 -
2 files changed, 53 insertions(+), 7 deletions(-)
diff --git a/fundamentals/middleware/problem-details-service/Controllers/ValuesController.cs b/fundamentals/middleware/problem-details-service/Controllers/ValuesController.cs
index e0be441..d8c7367 100644
--- a/fundamentals/middleware/problem-details-service/Controllers/ValuesController.cs
+++ b/fundamentals/middleware/problem-details-service/Controllers/ValuesController.cs
@@ -1,3 +1,42 @@
+#define FIRST // FIRST IMPROVED
+#if NEVER
+#elif FIRST
+//
+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));
+ }
+}
+//
+#elif IMPROVED
+//
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
}
}
+//
+#endif
diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs
index 113c2ef..ada0e8c 100644
--- a/fundamentals/middleware/problem-details-service/Program.cs
+++ b/fundamentals/middleware/problem-details-service/Program.cs
@@ -91,7 +91,6 @@ if (app.Environment.IsDevelopment())
}
app.UseHttpsRedirection();
-
app.UseStatusCodePages();
// Middleware to handle writing problem details to the response.