Url.Action return 浏览器中的当前 link

Url.Action return the current link in browser

我目前在页面 /Customer/Edit/13244

当我在同一页面上使用 @Url.Action("Edit", "Customer") 时,它 return 是我 /Customer/Edit/13244,但我希望它 return /Customer/Edit/.

请告诉我如何解决这个问题。

您将需要使用(假设您使用 id = UrlParameter.Optional

的默认路由
@Url.Action("Edit", "Customer", new { id = "" })

这是 MVC 的一个“特性”,很多人觉得不自然,was previously reported as a bug

微软官方回应:

Ultimately if you want the most control over what gets generated for a URL there are a few options to consider:

  1. Use named routes to ensure that only the route you want will get used to generate the URL (this is often a good practice, though it won't help in this particular scenario)
  2. Specify all route parameters explicitly - even the values that you want to be empty. That is one way to solve this particular problem.
  3. Instead of using Routing to generate the URLs, you can use Razor's ~/ syntax or call Url.Content("~/someurl") to ensure that no extra (or unexpected) processing will happen to the URL you're trying to generate.

实际上,当您尝试重新使用操作方法名称时,此错误只会露出丑陋的头。如果在后面没有跟id的情况下使用Edit以外的其他action方法名,这个问题就会神奇地消失