ASP.NET核心HttpGet单个Web API [英] ASP.NET core HttpGet single Web API

查看:90
本文介绍了ASP.NET核心HttpGet单个Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

我在设置HTTPGET并在Postman中测试解决方案时遇到困难。

I’m having difficulty setting up my HTTPGETs and then testing the solution in Postman.

我试图在两种情况下都返回单个结果,但是当我输入参数时,没有任何结果。因此,我显然缺少一些需要帮助的东西。

I’m trying to return a single result on both occasions however when I input the parameters nothing loads. So I'm clearly missing something which i need some help on please.

我的CashMovementController中有1个参数{id},如果我导航到localhost / api / cashmovements / {id}会加载,但是如果在邮递员中传递{id}参数会失败。

I have 1 parameter {id} in my CashMovementController and if I navigate to localhost/api/cashmovements/{id} it loads however if pass the {id} parameter in postman it fails.

然后在我的BondCreditRatingsController中,我有2个参数{ISIN}& {Date},我不确定如何处理此问题。

Then in my BondCreditRatingsController I have 2 parameters {ISIN} & {Date} and again I'm not sure how to approach this.

喜欢听听一些建议/帮助

Love to hear some advice/help on this please

感谢GWS

Startup.cs

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

CashMovementsController.cs

[Route("api/[controller]")]
public class CashMovementsController : Controller
{
    private ICashMovementRepository _cashmovementRepository;

    [HttpGet("{id}", Name = "GetCashMovement")]
    public IActionResult Get(int id)
    {

        CashMovement _cashmovement = _cashmovementRepository.GetSingle(u => u.CashMovementId == id);

        if (_cashmovement != null)
        {
            CashMovementViewModel _cashmovementVM = Mapper.Map<CashMovement, CashMovementViewModel>(_cashmovement);
            return new OkObjectResult(_cashmovementVM);
        }
        else
        {
            return NotFound();
        }
    }
}

BondCreditRatingsController.cs

[Route("api/[controller]")]
public class BondCreditRatingsController : Controller
{
    private IBondCreditRatingRepository _bondcreditratingRepository;


    public BondCreditRatingsController(IBondCreditRatingRepository bondcreditratingRepository)
    {
        _bondcreditratingRepository = bondcreditratingRepository;
    }

    [HttpGet("{id}", Name = "GetBondCreditRating")]
    public IActionResult Get(string id, DateTime efffectivedate)
    {

        BondCreditRating _bondcreditrating = _bondcreditratingRepository.GetSingle(u => u.ISIN == id, u => u.EffectiveDate == efffectivedate);

        if (_bondcreditrating != null)
        {
            BondCreditRatingViewModel _bondcreditratingVM = Mapper.Map<BondCreditRating, BondCreditRatingViewModel>(_bondcreditrating);
            return new OkObjectResult(_bondcreditratingVM);
        }
        else
        {
            return NotFound();
        }
    }


推荐答案

如果您要将其映射到api / Controller / method / id,则需要使用下面的代码,因为要将参数顺序(无其他标识符)映射到操作中的特定参数名称。

If you want to map it to api/Controller/method/id you would need to use the code below because you want to map parameter order (no other identifier) to a specific parameter name in the action.

[HttpGet("GetCashMovement/{id}")]

由于使用命名参数,并且无法将请求映射到任何其他模板,因此当前的代码应在下面使用。

Your current code should work with below since you are using named parameters and because the request can't be mapped to any other template.

/api/CashMovements/GetCashMovement?id=1

但是该属性语法也会(可能是无意地)触发:

But that attribute syntax will also (possibly unintentionally) trigger:

/api/CashMovements/1

由于您为该操作定义的模板的总和为:

Since a sum of your defined template for that action is:

[Route("api/[controller]/{id}")]

原因为什么/ api / ApiTest / GetCashMovement映射GetCashMovement.Get(int i)是因为id在启动时被定义为可选

Reason to why /api/ApiTest/GetCashMovement maps GetCashMovement.Get(int i) is because id is defined as optional in startup

routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/**{id?}**");




在路由参数名称定义可选字符后的问号(?)
参数。

A question mark (?) after the route parameter name defines an optional parameter.

https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/routing?view=aspnetcore-3.0 #create-routes

这篇关于ASP.NET核心HttpGet单个Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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