-1

Actually I'm working using dapper.

LoginAuditModel:

public class LoginAuditModel
    {
        public IList<GuidIdTableType> UserIdTableType { get; set; } = new List<GuidIdTableType>();
        public DateTime StartingDate { get; set; }
        public DateTime EndingDate { get; set; }
    }

Repository:

  public async Task<IEnumerable<LoginAuditGetViewModel>> LoginAuditGet(LoginAuditModel model)
        {
            try
            {
                async Task<IEnumerable<LoginAuditGetViewModel>> DoLoginAuditGet()
                {
                    using (var connection = _connectionManager.GetOpenConnection(_configuration.GetConnectionString(connectionstring)))
                    {
                        return await connection.QueryAsync<LoginAuditGetViewModel>("[dbo].[spName]", param: new
                        {
                            UserIdTableType = ((List<GuidIdTableType>)model.UserIdTableType).ToDataTable(),
                            model.StartingDate,
                            model.EndingDate
                        }
                          , commandType: CommandType.StoredProcedure);
                    }
                }
                return await DoLoginAuditGet();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Service:

 public async Task<IEnumerable<LoginAuditGetViewModel>> LoginAuditGet(LoginAuditModel model)
        {
            async Task<IEnumerable<LoginAuditGetViewModel>> DoLoginAuditGet()
            {
                return await _employeeRepository.LoginAuditGet(model);
            }
            return await DoLoginAuditGet();
        }

Controller:

[HttpGet]
    public async Task<IActionResult> LoginAuditGet([FromQuery]LoginAuditModel model)
    {
        try
        {
            async Task<IActionResult> DoLoginAuditGet()
            {
                var rModel = await _employeeService.LoginAuditGet(model);
                if (rModel is null || !rModel.Any()) return NotFound();
                return Ok(rModel);
            }
            return await DoLoginAuditGet();
        }
        catch (Exception ex)
        {
            return InternalServerError(ex);
        }
    }

When I execute this code using swagger in my case, table valued parameter is always passing with count = 0 (UserIdTableType), but for some reason, when I change controller method to [HttpPost] it pass parameter correctly! and everything it's working fine:

[HttpPost]
        public async Task<IActionResult> LoginAuditGet(LoginAuditModel model)
        {
            try
            {
                async Task<IActionResult> DoLoginAuditGet()
                {
                    var rModel = await _employeeService.LoginAuditGet(model);
                    if (rModel is null || !rModel.Any()) return NotFound();
                    return Ok(rModel);
                }
                return await DoLoginAuditGet();
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }

So, my question is why it is working as a Post method and not with Get? I need to change something to works with Get? Regards

Leon
  • 137
  • 9

1 Answers1

0

In your case, you want to send an array of objects as a query string in URL, I think this is impossible, but you can send an array of base data types such as int, string ... etc.

but in Post and Put, it is sending the data as body and have another type of data transfer.

and you should know, there is a limitation on Query String length, you can have a look here: click me

Rabea AlTaradeh
  • 213
  • 1
  • 9