通过路由调整地址栏中显示的 URL

发布于 2024-12-15 15:35:23 字数 653 浏览 1 评论 0原文

我想优化网络浏览器中显示的 URL。我知道路由非常适合,但我不知道它是否可以按照我想要的方式进行。我有一个列表中的项目列表。每个项目都有一个 ID、名称和类别。导航到详细产品页面时,显示的 URL 应为“/category/name”。我知道我可以在 ActionLink 中传递 ID、类别和名称,并在 Global.asax 中调整路由。我已经这样做了,但我仍然拥有与 URL 其余部分连接的 ID。这太丑了。

这是一个示例:

我认为的 ActionLink:

@Html.ActionLink(@p.Name, "Detail", new { projectID = @p.ProjectID, category = @p.Category, name = @p.Name })

Global.asax:

routes.MapRoute(null,
    "{category}/{name}",       
    new { controller = "Project", action = "Detail" }
);

生成的 URL:

,projectID 始终添加到 URL 的末尾。我怎样才能避免这种行为?

谢谢。

I would like to optimize my URL showed in web browsers. I know Routing is perfect for that but I don't know if it is possible the way I want. I have a list of projects in a list. Each project have an ID, a name and a category. When navigating to the detail product page, the URL showed should be "/category/name". I know I can pass the ID, the category and the name in the ActionLink and adjusting the routing in Global.asax. I already do that but I still have the ID which is concatenate to the rest of the URL. This is ugly.

Here is an example:

The ActionLink in my view:

@Html.ActionLink(@p.Name, "Detail", new { projectID = @p.ProjectID, category = @p.Category, name = @p.Name })

the Global.asax:

routes.MapRoute(null,
    "{category}/{name}",       
    new { controller = "Project", action = "Detail" }
);

The resulting URL:

enter image description here

As you can see, the projectID is always added to the end of the URL. How can I avoid that behaviour?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

回心转意 2024-12-22 15:35:23
routes.MapRoute(null,
"{category}/{name}/{projectID}",       
new { controller = "Project", action = "Detail", projectID = UrlParameter.Optional }

);

会给你的

/INDUSTRIE/Mailing/16

是你想要的吗?

routes.MapRoute(null,
"{category}/{name}/{projectID}",       
new { controller = "Project", action = "Detail", projectID = UrlParameter.Optional }

);

will give you

/INDUSTRIE/Mailing/16

is that what you want ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文