0

我是 asp.net mvc 的初学者。

我有 2 个控制器:

HomeController 动作: index,about

我需要的网址:

  index action url: mydomain

  about action url: mydomain/about

其他控制器动作:索引

  index action url: mydomain/other

MyCode 那不起作用

routes.MapRoute(
      "Other",
     "{controller}/{action}/{id}",
     new { controller = "Other", action = "Index", id = UrlParameter.Optional }
 );

  routes.MapRoute(
     name: "Default",
     url: "{action}/{id}",
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
 );

谢谢

4

2 回答 2

2

对于您的家庭控制器,点击

index action url: mydomain

你需要

routes.MapRoute(
 name: "Home",
 url: "",
 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

about action url: mydomain/about

你需要

routes.MapRoute(
 name: "Home",
 url: "about",
 defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
); 

对于您的其他控制器,点击

index action url: mydomain/other

你需要

routes.MapRoute(
 name: "Other",
 url: "other",
 defaults: new { controller = "Other", action = "Index", id = UrlParameter.Optional }
);

请注意,在所有情况下,“名称”参数并不重要。

于 2012-12-12T11:46:32.653 回答
-1

我认为您错过{controller}Default路由规范。

routes.MapRoute(
     name: "Default",
     url: "{controller}/{action}/{id}",
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
于 2012-12-12T11:40:37.930 回答