怪異,似乎無法重現該問題:
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
public ActionResult About(string id)
{
return View();
}
}
和內部Index.cshtml
:
@Url.Action("About", "Home")
現在,當我要求/home/index/123
網址助手產生/home/about
預期。沒有幻影參數。那麼你的情況如何不同?
UPDATE:
現在你已經澄清你的情況看來你具備以下條件:
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
}
和內部Index.cshtml
你要使用:
@Url.Action("Index", "Home")
如果您要求/home/index/123
這會生成/home/index/123
而不是預期的/home/index
(或簡單/
考慮到默認值)。
此行爲是設計使然。如果你想改變它,你將不得不編寫自己的幫手,忽略當前的路線數據。以下是它的外觀:
@UrlHelper.GenerateUrl(
"Default",
"index",
"home",
null,
Url.RouteCollection,
// That's the important part and it is where we kill the current RouteData
new RequestContext(Html.ViewContext.HttpContext, new RouteData()),
false
)
這會生成您期望的正確url。當然這很醜陋。我建議你將它封裝到一個可重用的幫助器中。使用的參數和供應空
不是這樣,嘗試爲沒有參數的同一頁面生成鏈接,但是請求帶有參數的頁面。所以在你的情況下,嘗試爲索引頁面本身生成鏈接。 –
@Threecoins,啊,好吧,明白了。這是設計。 –
哦,所以如果請求有參數,我不能在同一頁面內生成一個沒有參數的鏈接? –