Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to group my API actions into a small set of controllers so for example my KeyContoller would have get requests for the JWT Token and for the API Public key. The documentation I have read so far doesn't appear to allow that level of granularity and suggests seperate controllers.

What I have tried:

For the moment I can get by because the method signatures are different but I'm not happy with that. This is the controller code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CMDS.Controllers
{
    [Route("CMDS/[controller]")]
    [ApiController]
    public class KeyController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetPublicKey()
        {
            object returnData;
            returnData = "CMDS Public Key";
            return Ok(returnData);
        }
        [HttpGet("{AuthString}")]
        public IActionResult GetJWTToken(string AuthString)
        {
            object returnData;
            returnData = "JWT Token";
            return Ok(returnData);
        }
    }
}


Here are sample calls hitting each method:

for GetPublicKey
Invoke-RestMethod http://localhost:5000/CMDS/Key



for GetJWTToken
Invoke-RestMethod http://localhost:5000/CMDS/Key/"dDSD"
Posted
Updated 3-Mar-19 21:16pm

1 solution

I'm not sure if I understood quite what your after, but you can specify the get method signatures such as:

[HttpGet("GetPublicKey")]
public IActionResult GetPublicKey()
{
    object returnData;
    returnData = "CMDS Public Key";
    return Ok(returnData);
}
[HttpGet("GetJWTToken/{AuthString}")]
public IActionResult GetJWTToken(string AuthString)
{
    object returnData;
    returnData = "JWT Token";
    return Ok(returnData);
}


Which would allow:

Invoke-RestMethod http://localhost:5000/CMDS/Key/GetPublicKey

Invoke-RestMethod http://localhost:5000/CMDS/Key/GetJWTToken/"dDSD"
 
Share this answer
 
v3
Comments
Ger Hayden 4-Mar-19 3:38am    
I'll try that later today when I get the day job behind me. Its exactly what I am looking for.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900