ASP.NET 核心 HttpGet 单个 Web API

编程入门 行业动态 更新时间:2024-10-25 22:33:33
本文介绍了ASP.NET 核心 HttpGet 单个 Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

早安,

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

我试图在两种情况下都返回一个结果,但是当我输入参数时,什么都没有加载.所以我显然错过了一些我需要帮助的东西.

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

然后在我的 BondCreditRatingsController 我有 2 个参数 {ISIN} &{Date},我不知道该如何处理.

希望听到一些建议/帮助

感谢GWS

Startup.cs

app.UseMvc(routes =>{路线.MapRoute(名称:默认",模板:{controller=Home}/{action=Index}/{id?}");});

CashMovementsController.cs

[Route("api/[controller]")]公共类 CashMovementsController : 控制器{私人 ICashMovementRepository _cashmovementRepository;[HttpGet("{id}", Name = "GetCashMovement")]公共 IActionResult 获取(int id){CashMovement _cashmovement = _cashmovementRepository.GetSingle(u => u.CashMovementId == id);如果(_cashmovement != null){CashMovementViewModel _cashmovementVM = Mapper.Map(_cashmovement);返回新的 OkObjectResult(_cashmovementVM);}别的{返回未找到();}}}

BondCreditRatingsController.cs

[Route("api/[controller]")]公共类 BondCreditRatingsController : 控制器{私人 IBondCreditRatingRepository _bondcreditratingRepository;公共 BondCreditRatingsController(IBondCreditRatingRepository bondcreditratingRepository){_bondcreditratingRepository = bondcreditratingRepository;}[HttpGet("{id}", Name = "GetBondCreditRating")]公共 IActionResult 获取(字符串 id,日期时间有效日期){BondCreditRating _bondcreditrating = _bondcreditratingRepository.GetSingle(u => u.ISIN == id, u => u.EffectiveDate == efffectivedate);如果(_bondcreditrating != null){BondCreditRatingViewModel _bondcreditratingVM = Mapper.Map(_bondcreditrating);返回新的 OkObjectResult(_bondcreditratingVM);}别的{返回未找到();}}

解决方案

如果你想将它映射到 api/Controller/method/id 你需要使用下面的代码,因为你想映射参数顺序(没有其他标识符) 到动作中的特定参数名称.

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

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

/api/CashMovements/GetCashMovement?id=1

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

/api/CashMovements/1

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

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

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

routes.MapRoute(名称:默认",模板:{controller=Home}/{action=Index}/**{id?}**");

路由参数名称后的问号 (?) 定义了一个可选参数参数.

/docs.microsoft/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-3.0#create-routes

Good Morning,

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.

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.

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

Thanks 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(); } }

解决方案

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}")]

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.

docs.microsoft/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-3.0#create-routes

更多推荐

ASP.NET 核心 HttpGet 单个 Web API

本文发布于:2023-10-29 07:46:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   NET   ASP   API   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!