向邮递员发送参数 api 获取请求

Sending a paramater to postman api get request

我有一个控制器来计算大于某个日期的项目数。存储库显示为:

    public Dictionary<int, int> GetAllComplaintsCount(DateTime start)
    {
        try
        {
            return _context.Checklists
                .Where(a => a.COMPLAINT.Received_DT > start)
                .GroupBy(a => a.MonitorEnteredEmpID)
                .ToDictionary(g => g.Key, g => g.Count());
        }
        catch (Exception ex)
        {
            _logger.LogError("Could not get am with checklist", ex);
            return null;
        }
    }

编辑我已经包含了我的路由器以查看它是否正确:

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

问题 没有start参数,我可以通过postman获取http://localhost:8000/crams/api/counts。不过,我不确定如何通过邮递员合并日期,以便它只能提取大于开始的日期。

我试过了

http://localhost:8000/crams/api/counts/2016-1-1 but it comes back null.

您可以尝试将报价传递给您的 API

http://localhost:8000/crams/api/counts/636027305821590000

然后

public Dictionary<int, int> GetAllComplaintsCount(Int64 dateTicks)
    {
        try
        {
            var startDate = new DateTime(dateTicks);
            return _context.Checklists
                .Where(a => a.COMPLAINT.Received_DT > startDate)
                .GroupBy(a => a.MonitorEnteredEmpID)
                .ToDictionary(g => g.Key, g => g.Count());
        }
        catch (Exception ex)
        {
            _logger.LogError("Could not get am with checklist", ex);
            return null;
        }
    }

这里是关于如何从日期中获取刻度的信息(使用 JS)

How to convert JavaScript date object to ticks

希望这对您有所帮助:

/api/counts/2016-1-1 comes back as null

您的 API 是:

... GetAllComplaintsCount(DateTime start)

所以你的 url 应该是:

/api/counts?start=2016-1-1

这是默认路由。由于您没有在问题中包含路由,我假设(鉴于症状)它是默认设置:

/api/{controller}/{id}

即使用 /api/counts/2016-1-1 将“2016-1-1”指定为 id 参数,但您没有 id 参数并且您没有指定值对于 start,所以你得到 null。

您可以将路线添加为

/api/{controller}/{start}

我刚刚创建了一个 api 作为:

    [HttpGet]
    public string Test(DateTime d)
    {
        return d.ToString();
    }

并通过邮递员致电(尽管任何客户都会给出相同的结果)

http://localhost/api/../Test?d=2016-1-1

它返回了预期的“01/01/2016 00:00:00”