【发布时间】:2020-06-10 11:50:43
【问题描述】:
我在我的页面中使用以下 Razor:
<a asp-action="Logs" asp-route-channel="@channel.Name">
<button class="btn btn-success btn-circle"><i class="fa fa-eye"></i></button>
</a>
channel 是字符串,而不是整数,这就是我不使用id 的原因。
这就是我的路由方式:
endpoints.MapControllerRoute("logs", "Dashboard/Logs/{channel}");
在我的控制器中,我只是为了测试目的而这样做:
public IActionResult Logs(string channel)
{
return Content(channel);
}
但是,当链接生成时,我得到这样的东西:
<a href="/Dashboard/Logs?channel=mychannel">
而不是预期的:
<a href="/Dashboard/Logs/mychannel">
编辑
这是我尝试过的:
[HttpGet("{channel}")]
public IActionResult Logs(string channel)
{
return Content(channel);
}
导致https://localhost:44351/mychannel
我也试过了:
[HttpGet("Dashboard/Logs/{channel}")]
public IActionResult Logs(string channel)
{
return Content(channel);
}
哪个按预期工作,但为什么我必须像这样包含整个路径?
【问题讨论】:
-
为什么不使用属性路由?
-
那我得写
[HttpGet("Dashboard/Logs/{channel}")],这看起来有点傻,因为它应该知道我的Controller和Action是什么。 -
您正在映射到默认路由,该路由很可能具有
id。显示所有映射并包括控制器定义。
标签: asp.net asp.net-mvc asp.net-core routes