需要 mvc 和 方面的帮助;路线

发布于 2024-08-07 23:11:59 字数 879 浏览 11 评论 0原文

我对 MVC 很陌生,我正在尝试使用它来设置一个新站点。出于 SEO 的原因,我们需要将页面的 url 设置为“Recruiter/4359/John_Smith”或基本上 {controller}/{id}/{name}。当我在后面的代码中创建 url 时,我就可以这样工作了...

//r is a recruiter object that is part of the results for the view

r.Summary = searchResult.Summary + "... <a href=\"/Recruiter/" + r.Id + "/" + r.FirstName + "_" + r.LastName + "\">Read More</a>"

但是当我在视图中使用搜索结果集合并迭代它们时,我试图创建另一个指向同一页面的链接来做某事就像 <%=Html.ActionLink(x => x.Detail((int)r.Id), r.RecruiterName)%> 但这不起作用。当我在视图中使用该代码时,它会给我一个 /Recruiter/Detail/4359 形式的 url ,一位同事告诉我应该使用 Html.ActionLink 在视图和控制器中创建链接,以便如果将来路线发生变化,它会自动工作。不幸的是,他不确定在这种情况下该怎么做。所以,我的问题是...

  1. 如何使 Html.ActionLink 在视图中工作以创建我需要的 url(如上面的 r.Summary)?
  2. 如何在控制器中使用 Html.ActionLink 而不是像上面那样对链接进行硬编码?

I'm very new to MVC and I'm trying to get a new site set up using it. For SEO reasons we need to make the url of a page something like "Recruiter/4359/John_Smith" or basically {controller}/{id}/{name}. I have that working when I create the url in the code behind like so...

//r is a recruiter object that is part of the results for the view

r.Summary = searchResult.Summary + "... <a href=\"/Recruiter/" + r.Id + "/" + r.FirstName + "_" + r.LastName + "\">Read More</a>"

But when I am using the collection of results from a search in my view and iterating through them I am trying to create another link to the same page doing something like <%=Html.ActionLink<RecruiterController>(x => x.Detail((int)r.Id), r.RecruiterName)%> but that doesn't work. When I use that code in the view it gives me a url in the form of /Recruiter/Detail/4359 I was told by a coworker that I should use the Html.ActionLink to create the link in both the view and the controller so that if the route changes in the future it will automatically work. Unfortunately he wasn't sure how to do that in this case. So, my problems are...

  1. How can I make the Html.ActionLink work in the view to create a url like I need (like r.Summary above)?
  2. How do I use the Html.ActionLink in a controller instead of hardcoding the link like I have above?

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

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

发布评论

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

评论(2

沫雨熙 2024-08-14 23:11:59

我偶然发现了这篇博文,它让我朝着正确的方向前进。

http: //www.chadmoran.com/blog/2009/4/23/optimizing-url- Generation-in-aspnet-mvc-part-2.html

I came across this blog post which got me going in the right direction.

http://www.chadmoran.com/blog/2009/4/23/optimizing-url-generation-in-aspnet-mvc-part-2.html

独自←快乐 2024-08-14 23:11:59

最好按照您同事的说法使用 ActionLink 方法写出链接,这样它们将始终与您的路线匹配。

在您当前的情况下,它写出该方法的原因是因为它基于默认路由。您可以通过在 Global.asax 中的默认路由之上添加另一条路由来解决此问题。您只需要像这样规定您想要的格式:

routes.MapRoute(
    "Recruiter",
    "Recruiter/{id}/{name}",
    new { controller = "Recruiter", action = "Details" }
);

MVC 将按照注册的顺序处理您的路由,因此将其放在默认值之前将使其使用您的路由。

编辑:

您可能会发现此路线调试工具很有用。

It is a good idea to use the ActionLink method to write out links as your coworker says, that way they will always match your routes.

In your current case the reason it is writing out the method is because it is based on the default routing. You can fix this by adding another route above the default one in the Global.asax. You just need to stipulate the format you want like this:

routes.MapRoute(
    "Recruiter",
    "Recruiter/{id}/{name}",
    new { controller = "Recruiter", action = "Details" }
);

MVC will work through your routes in the order they are registered so putting this before the default will make it use your route instead.

EDIT:

You might find this route debugging tool useful.

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