How to include Query String in the route resolution in order to allow multiple Actions with the same Method, Route and Query String?

后端 未结 2 1140
醉酒成梦
醉酒成梦 2021-01-24 13:03

I am converting an ASP.NET MVC (.NET Framework) application to ASP.NET Core MVC. This is strictly a conversion, I cannot make any breaking changes hence I cannot change any Rout

2条回答
  •  猫巷女王i
    2021-01-24 13:19

    Why not use a single endpoint instead? You don't need to pass Guid's, since it's a GET operation, you can pass strings and cast them later. That way you can send one parameter or the other.

    [HttpPut]
    [Route("status")]
    public async Task UpdateStatus([FromBody] POST_Status statusModel, [FromQuery] string orderGUID = null, [FromQuery] string id = null)
    {
        if (!string.IsNullOrEmpty(orderGUID))
        {
            // UpdateStatusByOrderGuid implementation here
            // Guid guid = Guid.Parse(orderGUID);
    
        }
        else if (!string.IsNullOrEmpty(id))
        {
            // UpdateStatusById implementation here
            // Guid guid = Guid.Parse(id);
        }
        else
        {
            throw new ArgumentException("No valid GUID.");
        }
    }
    

    This endpoint should be compatible with both scenarios you specified.

提交回复
热议问题