Advertisement
yuvarajupadhyaya

Responses In API core

Aug 18th, 2022
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | Source Code | 0 0
  1. using CourseService.API.ModelBinders;
  2. using Microsoft.AspNetCore.Authentication.JwtBearer;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6.  
  7.  
  8.  
  9. namespace CourseService.API.Controllers
  10. {
  11.     /// <summary>
  12.     /// Main controller with authorization added in annotation, this is core controller for authorization of user
  13.     /// </summary>
  14.     [ApiController]
  15.     [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
  16.     public class Base : ControllerBase
  17.     {
  18.         /// <summary>
  19.         /// Response
  20.         /// </summary>
  21.         public ResponseModel response;
  22.  
  23.  
  24.  
  25.        /// <summary>
  26.         ///
  27.         /// </summary>
  28.         public Base()
  29.         {
  30.             response = new ResponseModel();
  31.         }
  32.  
  33.  
  34.  
  35.        /// <summary>
  36.         /// Returns Ok result
  37.         /// </summary>
  38.         /// <returns></returns>
  39.         [NonAction]
  40.         public IActionResult MeshedOk()
  41.         {
  42.             //response.Status = response.Status == ResponseStatus.Error ? ResponseStatus.Error : ResponseStatus.Success;
  43.             return Ok(response);
  44.         }
  45.  
  46.  
  47.  
  48.        /// <summary>
  49.         ///
  50.         /// </summary>
  51.         /// <returns></returns>
  52.         [NonAction]
  53.         public IActionResult MeshedCreated()
  54.         {
  55.             response.Status = ResponseStatus.Success;
  56.  
  57.  
  58.  
  59.            return StatusCode(StatusCodes.Status201Created, response);
  60.         }
  61.  
  62.  
  63.  
  64.        /// <summary>
  65.         ///
  66.         /// </summary>
  67.         /// <param name="actionName"></param>
  68.         /// <param name="route"></param>
  69.         /// <returns></returns>
  70.         [NonAction]
  71.         public IActionResult MeshedCreatedAtAction(string actionName, object route)
  72.         {
  73.             response.Status = ResponseStatus.Success;
  74.  
  75.  
  76.  
  77.            return CreatedAtAction(actionName, route, response);
  78.         }
  79.  
  80.  
  81.  
  82.        /// <summary>
  83.         /// Returns not found
  84.         /// </summary>
  85.         /// <returns></returns>
  86.         [NonAction]
  87.         public IActionResult MeshedNotFound()
  88.         {
  89.             response.Status = ResponseStatus.Error;
  90.             return NotFound(response);
  91.         }
  92.  
  93.  
  94.  
  95.        /// <summary>
  96.         ///
  97.         /// </summary>
  98.         /// <returns></returns>
  99.         [NonAction]
  100.         public IActionResult MeshedBadRequest()
  101.         {
  102.             response.Status = ResponseStatus.Error;
  103.             return BadRequest(response);
  104.         }
  105.  
  106.  
  107.  
  108.        /// <summary>
  109.         ///
  110.         /// </summary>
  111.         /// <returns></returns>
  112.         [NonAction]
  113.         public IActionResult MeshedError()
  114.         {
  115.             response.Status = ResponseStatus.Error;
  116.             return StatusCode(StatusCodes.Status500InternalServerError, response);
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement