1.0 KiB
1.0 KiB
LogicResults
LogicResults provide another layer of abstraction above the ActionResults.
They help you sending the right HttpStatusCode with the right data.
Usage
-
Create an endpoint that returns an
ActionResult:[HttpGet("hello")] public ActionResult<string> Hello() { return new ActionResult<string>("Hello, World!"); } -
Now instead of directly returning the
ActionResult, return aLogicResult[HttpGet("hello")] public ActionResult<string> Hello() { return LogicResult<string>.Ok("Hello, World!"); } -
This allows you to very easily change the return type by simply calling the right function
[HttpGet("hello")] public ActionResult<string> Hello() { if (!Auth.IsLoggedIn) return LogicResult<string>.Forbidden(); return LogicResult<string>.Ok("Hello, World!"); }Hint: You can also provide an error message for status codes that are not in the 200 range.