2010-08-20 45 views
4

我想有设置路线如下:在ASP.NET MVC 2应用类似Twitter路由

xyz/映射到一个处理法没有参数,但xyz/{username}映射到一个不同的操作(在相同的控制器或不,无关紧要),它接受一个名为字符串类型的用户名的参数。下面是我的路线至今:

routes.MapRoute(
    "Me", 
    "Profile", 
    new { controller = "Profile", action = "Me" } 
); 

routes.MapRoute(
    "Profile", 
    "Profile/{username}", 
    new { controller = "Profile", action = "Index" } 
); 

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

目前,如果我浏览到/Profile/someuser,在配置文件控制器Index操作如预期的打击。但如果我导航到/Profile//Profile,我得到了404个。

什么给?

+0

哇,对不起,我不小心把Me()方法设置为private。发现太久了。我讨厌这样的错误,你把你的头发拉出几个小时,结果变成一团糟。我确实怀疑这会是一件愚蠢的事情,但我真的很机智。 – gzak 2010-09-04 00:54:20

回答

4

它适用于我。我想你的Profile控制器中没有Me动作方法?

我试图用以下途径:

routes.MapRoute(
    "Me", 
    "Profile", 
    new { controller = "Home", action = "Me" } 
); 

routes.MapRoute(
    "Profile", 
    "Profile/{username}", 
    new { controller = "Home", action = "Index" } 
); 

通过这样定义的IndexMe动作方法:

public ActionResult Index(string username) { 
    ViewData["Message"] = username; 
    return View(); 
} 

public ActionResult Me() { 
    ViewData["Message"] = "this is me!"; 
    return View("Index"); 
} 

我使用的默认Home/Index视图。

+0

+1为无法复制。评论“Me()”会导致404错误。您可能是因为控制器中缺少Me()。 – 2010-08-21 02:08:22