小编luk*_*pep的帖子

带有可选参数/默认参数值的ASP.NET5 MVC 6 routung

我正在VS2015,C#,ASP.NET5 MVC6堆栈中构建Restful样式API,在路由方面我遇到了一些麻烦.我直接在控制器中使用注释:

[HttpGet("machine/{machineId}-{cnt:int}")]
public IActionResult GetReportsByMachineId(string machineId, int cnt)
{
    var item = _reportRepository.GetReportsByMachineId(machineId, 0, cnt);
    if ((item == null) || (!item.Any()) )
    {
        return HttpNotFound();
    }
    return new ObjectResult(item);
}
Run Code Online (Sandbox Code Playgroud)

它在我打电话时起作用api/report/machine/[machineName]-5- 我从数据库收到5个文件.

但我想5使用默认值使参数可选 - 所以调用cnt将是有效的.

基于http://stephenwalther.com/archive/2015/02/07/asp-net-5-deep-dive-routing我到目前为止尝试过:

api/report/machine/[machineName]- 控制器没有捕获[HttpGet("machine/{machineId}-{cnt:int=10}")](我得到欢迎页面代码)

api/machine/{machineName} - 应用程序无法启动(我甚至没有获得MVC欢迎页面)

[HttpGet("machine/{machineId}-{cnt?}")] - 应用程序无法启动(如上所述)

有任何想法吗?

整个控制器的代码(我没有指定任何其他路线):http: //pastebin.pl/view/11050284

c# asp.net-core asp.net-core-routing

3
推荐指数
1
解决办法
3519
查看次数

标签 统计

asp.net-core ×1

asp.net-core-routing ×1

c# ×1