我有一个带有RouteAttribute的ASP.NET核心端点:
[HttpGet]
[Route("MyController/MyAction/{id}")]
public async Task<IActionResult> GetAsync(int id, string rc)
{
...请注意,我希望将id作为URL的一部分传递,并将rc作为查询字符串传递。
我有一个MVC剃刀页面,它应该使用锚助手来创建到这个控制器的链接:
@foreach (var item in Model)
{
<a asp-controller="MyController" asp-action="MyAction"
asp-route-id="@item.Id" asp-route-rc=@item.Rc>Execute</a>
}我希望这会创建一个带有链接的锚:
http://localhost:5000/MyController/MyAction/1?rc=234而是创建一个带有链接的锚点:
http://localhost:5000/MyController/MyAction?id=1&rc=234换句话说,它将id作为查询字符串发送,而不是作为URL的一部分,尽管RouteAttribute中有模板声明。
有什么想法可以解释原因吗?
发布于 2021-08-01 22:46:54
如果您希望端点按照您想要的方式工作,则必须在启动时像这样配置端点
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});当您在默认路由中使用id时,html helper会将默认路由中的id值放在id处。您可以使用其他名称而不是id。然后你就可以在html助手中使用这个名字了。非默认名称将作为查询字符串参数添加到url。
如果在你的初创阶段
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});那么你应该主要使用属性路由
https://stackoverflow.com/questions/68614833
复制相似问题