我正在尝试创建一个类似https://localhost:44332/training/course/1/buy
的链接。下面是我生成它的代码:
.cshtml:
<a asp-controller="training"
asp-action="course"
asp-route-id="1"
asp-route-type="buy">Buy now</a>
Startup.cs:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "Buy",
pattern: "{controller=Home}/{action=Index}/{id?}/{type=buy}");
endpoints.MapRazorPages();
});
但是它会生成类似https://localhost:44332/training/course/1?type=buy
谁能告诉我,我在哪里弄错了?如何生成我的预期链接?
答案 0 :(得分:0)
您需要将自定义路由映射放在默认路由映射之前,然后将buy
中的{type=buy}
替换为{type=test}
之类的单词,或者只使用{type}
最终代码:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "Buy",
pattern: "{controller=Home}/{action=Index}/{id?}/{type=test}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});