【发布时间】:2017-04-13 14:38:08
【问题描述】:
我在 Asp.net 核心中阅读了很多关于 API 路由的主题,但我无法使其工作。
首先,这是我的控制器:
Public class BXLogsController : Controller
{
//[HttpGet("api/[controller]/ID/{id}", Name = "GetL")]
public IActionResult GetById(string id)
{
if (id.Trim() == "")
return BadRequest();
else
{
Logs l = AccessBase.AccBase.GetLog(id);
return Json(l);
}
}
//[HttpGet("api/[controller]/API/{apiname}", Name = "GetLAPI")]
public IActionResult GetByAPI(string apiname)
{
if (apiname.Trim() == "")
return BadRequest();
else
{
List<Logs> lstLogs = AccessBase.AccBase.GetLogsApi(apiname);
return Json(lstLogs);
}
}
}
我尝试将HttpGetAttribute 与路径一起使用(请参阅评论),但这不起作用。
所以我想使用MapRoute 方法,但这也不起作用。
app.UseMvc(routes =>
{
routes.MapRoute(
name: "LogsId",
template: "api/[controller]/ID/{id}",
defaults: new { controller = "BXLogs", action = "GetById" });
routes.MapRoute(
name: "LogsAPI",
template: "api/[controller]/API/{apiname}",
defaults: new { controller = "BXLogs", action = "GetByAPI" });
});
我一定忘记了什么,但我什么也没看到。
谁能帮帮我?
【问题讨论】:
-
确保没有重复的路线。
标签: c# asp.net-core .net-core asp.net-core-webapi asp.net-core-routing