2012-08-06 55 views
1

我有家庭控制器。我的家庭控制器有两个索引方法之一没有参数和一个参数。它像方法在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://本地主机:7221HTTP: //本地主机: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

我无法捕获错误。我的路由代码有什么问题。请帮忙。谢谢

回答

1

您不能在MVC中使用方法重载操作。如果你详细说明你想要达到的目标,你会得到有关如何做到这一点的建议。

+0

请告诉我为什么方法过载不可能的情况下mvc行动。 – Thomas 2012-08-06 16:42:29

+0

@Thomas简而言之,“这种行为是通过设计的”。路由工作方式和解析动作名称的方式可防止使用重载,因为根据定义,重载共享相同的名称,并且路由中的操作仅为*名称,因此无法找到正确的方法。 – 2012-08-06 16:51:20

+0

@Thomas:你的问题解决了吗? – 2012-10-02 12:51:07