【问题标题】:Twitter-like routes in ASP.NET MVC 2 applicationASP.NET MVC 2 应用程序中的类似 Twitter 的路由
【发布时间】:2011-04-01 21:53:25
【问题描述】:

我想按如下方式设置路线:

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,配置文件控制器中的索引操作会按预期命中。但如果我导航到 /Profile//Profile,我会得到 404。

什么给了?

【问题讨论】:

  • 哇,抱歉,我不小心将 Me() 方法设置为私有。这花了太长时间才发现。我讨厌这样的虫子,你把头发拉出来几个小时,结果却是微不足道的。我实际上怀疑它会像那样愚蠢,但我无计可施。

标签: c# .net asp.net-mvc-2 routing


【解决方案1】:

它对我来说很好用。我想你的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 视图。

【讨论】:

  • +1 用于非复制。评论 Me() 会导致 404。您可能对控制器中缺少 Me() 是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-05
  • 2017-01-12
  • 2011-07-02
  • 2020-01-19
  • 2018-12-14
  • 1970-01-01
  • 2011-08-05
相关资源
最近更新 更多