2011-02-04 62 views
1

我有以下ActionMethod在UserController中默認指數法行爲的ASP.NET MVC

public ActionResult Index(string id, string name, int? org)

當我導航到>http://example.com/User,上述操作方法被調用。那很好。

但是,當我導航到>http://example.com/User/1時,它找不到資源。它不應該導航到id = 1的上述操作方法,其餘的爲null?

路由在Global.asax中:

context.MapRoute( 
    "Default", 
    "/{controller}/{action}/{id}", 
    new { action = "Index", id = UrlParameter.Optional } 
); 
+0

你在global.asax中看起來像什麼? – Jon 2011-02-04 12:01:39

回答

2

你將不得不與其他參數添加到您的路由,以及爲他們曾經得到填充。

routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}/{name}/{org}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional, org = UrlParameter.Optional } // Parameter defaults 
      ); 

然後,您可以導航到http://yourdomain/User/Index/1

姓名和組織都是可選的,你也可以在需要時在通過這些

http://yourdomain/User/Index/1/fred

http://yourdomain/User/Index/1/fred/44