ASP.NET MVC路由问题

Kev*_*ang 13 asp.net-mvc routing

我必须密集.在对StackOverflow提出几个问题后,在掌握ASP.NET MVC提供的新路由引擎时,我仍然感到茫然.我想我已经把问题缩小到一个非常简单的问题,如果解决了,可能会让我解决其余的路由问题.所以这里是:

您将如何注册支持类似Twitter的用户配置文件的路由?

www.twitter.com/username

假设还需要支持:

  • 默认{controller}/{action}/{id}路由.

  • 网址如:

    www.twitter.com/login
    www.twitter.com/register

这可能吗?

giu*_*ius 10

关于什么

routes.MapRoute(
    "Profiles",
    "{userName}",
    new { controller = "Profiles", action = "ShowUser" }
);
Run Code Online (Sandbox Code Playgroud)

然后,在ProfilesController中,会有一个函数

public ActionResult ShowUser(string userName)
{
...
Run Code Online (Sandbox Code Playgroud)

在函数中,如果找不到具有指定userName的用户,则应该重定向到默认的{controller}/{action}/{id}(这里,它只是{controller})route.

像www.twitter.com/login这样的网址应该在那之前注册.

routes.MapRoute(
    "Login",
    "Login",
    new { controller = "Security", action = "Login" }
);
Run Code Online (Sandbox Code Playgroud)