为什么我的路由 URL 在不同的控制器中不匹配?

发布于 2024-10-04 05:22:38 字数 1027 浏览 3 评论 0原文

我刚刚创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若有似无的小暗淡 2024-10-11 05:22:38

您的路线无效。可选参数只能出现在最后。在您的情况下,您有 cat 路由参数,它是选项,位于 url 的中间。因此,如果您希望选择此路由而不是默认路由,则需要为此参数提供一个值:

Url.Action("Details", "Products", 
    new { id = "123", productName = "foo", cat = "bar" })

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:

Url.Action("Details", "Products", 
    new { id = "123", productName = "foo", cat = "bar" })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文