Controller Query Parameter (not in action)

怎甘沉沦 提交于 2021-02-11 17:01:22

问题


I'm trying to make a api like the OPENBANKPROJECT. Such as /api/banks/{BANK_ID}/atms/{ATM_ID} etc. I guess banks and atm is a different controller

I want get a global (api/Claim/{id}/Detail/[action] i need this {id}) parameter before before [action] initialize (maybe in constructor).

How do i get this {id} before [action] initialize?

    [Route("api/Claim/{id}/Detail/[action]")]
    public class ClaimDetailController
    {
        int _id; // assignment {id} 
        public ClaimDetailController(IClaimDetailService claimDetailService)
        {
           `Need Query Id before execute action`
        }
        [HttpPost]
        public async Task<BaseResponse> ClaimDetailInfoPolicy(ClaimDetailKeyModel model)
        {
            return `codes with _id`;
        }
    }

    public class ClaimDetailKeyModel
    {
        public long FileNo { get; set; }
        public long RecourseNo { get; set; }
    }

回答1:


Solution was simple :)

[ApiController]
[Route("api/Claim/{claimId}/Detail/[action]/")]
public class ClaimDetailController
{
    [FromRoute(Name = "claimId")] 
    public int Id { get; set; }
    public ClaimDetailController(IClaimDetailService claimDetailService)
    {
      `bla bla`
    }

    [HttpPost]
    public async Task<BaseResponse> ClaimDetailInfoPolicy(ClaimDetailKeyModel model)
    {
        return `codes with Id`
    }
}



回答2:


Add this id to your request handler.

[HttpPost]
public async Task<BaseResponse> ClaimDetailInfoPolicy(int id, ClaimDetailKeyModel model)
{
    return `codes`;
}


来源:https://stackoverflow.com/questions/60072596/controller-query-parameter-not-in-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!