我有家庭控制器。我的家庭控制器有兩個索引方法之一沒有參數和一個參數。它像方法在homecontroller加載和asp.net中的路由問題mvc
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View("index");
}
public ActionResult Index(int a)
{
ViewData["Message"] = "Welcome to ASP.NET MVC! and Your Age is " + a;
return View();
}
我有這樣定義
routes.MapRoute(
"Default1", // Route name
"{Home}/{ID}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
我想在Global.asax中只有兩個路由條目,當我鍵入URL喜歡的http://本地主機:7221或HTTP: //本地主機:7221 /家庭則index()方法,沒有PARAM家用控制器的應激發和
當i型的http://本地主機:7221 /家庭/ 77那麼帶有主控制器參數的索引(int a)方法應該會觸發。但我得到錯誤,爲什麼我輸入兩種我指定的url。錯誤消息是 控制器類型'HomeController'的當前操作索引'Index'在以下操作方法之間不明確: System.Web.Mvc.ActionResult Indexner()類型爲BeginnerMVC.Controllers.HomeController System.Web。 Mvc.ActionResult索引(Int32)類型BeginnerMVC.Controllers.HomeController
我無法捕獲錯誤。我的路由代碼有什麼問題。請幫忙。謝謝
請告訴我爲什麼方法過載不可能的情況下mvc行動。 – Thomas 2012-08-06 16:42:29
@Thomas簡而言之,「這種行爲是通過設計的」。路由工作方式和解析動作名稱的方式可防止使用重載,因爲根據定義,重載共享相同的名稱,並且路由中的操作僅爲*名稱,因此無法找到正確的方法。 – 2012-08-06 16:51:20
@Thomas:你的問題解決了嗎? – 2012-10-02 12:51:07