为什么我的路由 URL 在不同的控制器中不匹配?
我刚刚创建了一个 SiteMap 控制器,它为我的站点生成站点地图。我正在尝试输出我在整个网站上列出的产品的网址。例如,在我的产品列表视图中,我有以下代码:
Url.Action("Details", "Products", new { id = item.Id, productName = Url.ToFriendlyUrl(item.Brand + " " + item.Colour + " " + item.Size + " " + item.Title) })
输出此网址:
/products/details/1330/sandee-blue-small-camo-shorts,
映射到此路线:
routes.MapRoute(
"ViewProduct",
"products/{cat}/{id}/{productName}",
new { controller = "products", action = "Details", cat = "", id = "", productName = "" },
new string[] { "EliteFightKit.Web.Controllers" }
);
在我的 SiteMap 控制器中,但是我正在使用此代码生成网址:
Url.Action("Details", "Products", new { id = item.Id, productName = Url.ToFriendlyUrl(item.Brand + " " + item.Colour + " " + item.Size + " " + item.Title) })
它输出以下内容:
/products/details/1330?productname=sandee-blue-small-camo-shorts
我哪里出错了,如何让 SiteMap url 与我的产品中创建的 url 相关联列表视图?
劳埃德
I've just created a SiteMap controller which generates a sitemap for my site. I'm trying to output the url of the products that I have listed throughout the site. In my products list view for example I have this code:
Url.Action("Details", "Products", new { id = item.Id, productName = Url.ToFriendlyUrl(item.Brand + " " + item.Colour + " " + item.Size + " " + item.Title) })
which outputs this url:
/products/details/1330/sandee-blue-small-camo-shorts
which maps to this route:
routes.MapRoute(
"ViewProduct",
"products/{cat}/{id}/{productName}",
new { controller = "products", action = "Details", cat = "", id = "", productName = "" },
new string[] { "EliteFightKit.Web.Controllers" }
);
In my SiteMap controller however I'm using this code to generate the url:
Url.Action("Details", "Products", new { id = item.Id, productName = Url.ToFriendlyUrl(item.Brand + " " + item.Colour + " " + item.Size + " " + item.Title) })
and it's outputting this:
/products/details/1330?productname=sandee-blue-small-camo-shorts
where am I going wrong, how do I get the SiteMap url to correlate with the url created in my products list view?
Lloyd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的路线无效。可选参数只能出现在最后。在您的情况下,您有
cat
路由参数,它是选项,位于 url 的中间。因此,如果您希望选择此路由而不是默认路由,则需要为此参数提供一个值:Your route is not valid. Optional parameters can only appear at the end. In your case you have the
cat
route parameter which is option and which is in the middle of the url. So you need to provide a value for this parameter if you want this route to be picked instead of the default one: