Url.Action() 不删除默认值
Url.Action() does not remove default values
我有一个比较简单的路由映射。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{seoName}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, seoName = UrlParameter.Optional }
);
现在,如果使用 Url.Action("Index", "Home")
,它不会正确删除路由的默认值。它给了我 /Home/Index
.
现在,如果我删除 {id}
或 {seoName}
及其相应的默认值,那么 URL 会像 /
(root)一样正确生成。
我在这里缺少什么?它似乎不是环境值,因为我正在访问主页时没有 ID,也没有 seoNames。
有什么想法吗?
您将需要多个映射来实现您想要的,因为您只能将最后一个路由占位符设为可选。
routes.MapRoute(
name: "SeoFriendly",
url: "{controller}/{action}/{id}/{seoName}",
defaults: new { controller = "Home", action = "Index", seoName = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
我有一个比较简单的路由映射。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{seoName}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, seoName = UrlParameter.Optional }
);
现在,如果使用 Url.Action("Index", "Home")
,它不会正确删除路由的默认值。它给了我 /Home/Index
.
现在,如果我删除 {id}
或 {seoName}
及其相应的默认值,那么 URL 会像 /
(root)一样正确生成。
我在这里缺少什么?它似乎不是环境值,因为我正在访问主页时没有 ID,也没有 seoNames。
有什么想法吗?
您将需要多个映射来实现您想要的,因为您只能将最后一个路由占位符设为可选。
routes.MapRoute(
name: "SeoFriendly",
url: "{controller}/{action}/{id}/{seoName}",
defaults: new { controller = "Home", action = "Index", seoName = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);