Asp.net 路由url包括哪些内容=
我在我的页面中使用以下剃须刀:Asp.net 路由url包括哪些内容=,asp.net,asp.net-mvc,asp.net-core,routes,Asp.net,Asp.net Mvc,Asp.net Core,Routes,我在我的页面中使用以下剃须刀: <a asp-action="Logs" asp-route-channel="@channel.Name"> <button class="btn btn-success btn-circle"><i class="fa fa-eye"></i></button> </a> 在我的控制器中,我只是出于测试目的: public IActionResult Logs(string c
<a asp-action="Logs" asp-route-channel="@channel.Name">
<button class="btn btn-success btn-circle"><i class="fa fa-eye"></i></button>
</a>
在我的控制器中,我只是出于测试目的:
public IActionResult Logs(string channel)
{
return Content(channel);
}
但是,当链接生成时,我得到如下结果:
<a href="/Dashboard/Logs?channel=mychannel">
这将导致https://localhost:44351/mychannel
我也试过:
[HttpGet("Dashboard/Logs/{channel}")]
public IActionResult Logs(string channel)
{
return Content(channel);
}
它按预期工作,但为什么我必须像那样包含整个路径?您正在映射到默认的基于约定的路由,它很可能将
{id}
作为路由参数。因为它映射到默认值,并且您包含了{channel}
,所以它会将该参数添加为查询字符串,而不是URL的一部分
您需要为该操作包含一个自定义路由,以便生成所需的URL
endpoints.MapControllerRoute(
name: "Logs",
pattern: "Dashboard/Logs/{channel}",
defaults: new { controller = "Dashboard", action = "Logs" });
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
需要包括必要的路线值,以便它映射到预期路线
<a asp-controller="Dashboard" asp-action="Logs" asp-route-channel="@channel.Name">
<button class="btn btn-success btn-circle"><i class="fa fa-eye"></i></button>
</a>
为什么不使用属性路由?然后我必须编写[HttpGet(“Dashboard/Logs/{channel}”)]
,这似乎有点愚蠢,因为它应该知道我的控制器和操作是什么。您正在映射到默认路由,它很可能具有id
。显示所有映射,并包括控制器定义。
[HttpGet("Dashboard/Logs/{channel}")]
public IActionResult Logs(string channel)
{
return Content(channel);
}
endpoints.MapControllerRoute(
name: "Logs",
pattern: "Dashboard/Logs/{channel}",
defaults: new { controller = "Dashboard", action = "Logs" });
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
<a asp-controller="Dashboard" asp-action="Logs" asp-route-channel="@channel.Name">
<button class="btn btn-success btn-circle"><i class="fa fa-eye"></i></button>
</a>