如何将.NET核心API请求路由到特定的控制器方法? [英] How do I route a .NET core API request to a specific controller method?

查看:334
本文介绍了如何将.NET核心API请求路由到特定的控制器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的API操作分组到一小组控制器中,例如我的KeyContoller会获得JWT令牌和API公钥的请求。我到目前为止阅读的文档似乎没有允许这种级别的粒度,并建议单独的控制器。



我尝试过:



目前我可以通过,因为方法签名不同但我对此并不满意。这是控制器代码:

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Mvc;

名称空间CMDS.Controllers
{
[路由(CMDS / [控制器])]
[ApiController]
公共类KeyController:ControllerBase
{
[HttpGet]
public IActionResult GetPublicKey()
{
object returnData;
returnData =CMDS Public Key;
返回Ok(returnData);
}
[HttpGet({AuthString})]
public IActionResult GetJWTToken(string AuthString)
{
object returnData;
returnData =JWT Token;
返回Ok(returnData);
}
}
}





以下是针对每种方法的示例调用:
GetPublicKey




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

<对于GetJWTToken,






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

解决方案

我不确定我是否完全了解你的事情,但你可以指定获取方法签名如:



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





哪会允许:



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



 Invoke-RestMethod http: //本地主机:5000 / CMDS /键/ GetJWTToken / DDSD


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"

解决方案

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"


这篇关于如何将.NET核心API请求路由到特定的控制器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆