ASP.NET MVC 像 StackOverflow 中那样的用户路由? [英] ASP.NET MVC User routing like that in StackOverflow?

查看:15
本文介绍了ASP.NET MVC 像 StackOverflow 中那样的用户路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了 StackOverflow 上的路由,我有一个非常菜鸟的问题,但我仍然想澄清一些问题.

I've looked at the routing on StackOverflow and I've got a very noobie question, but something I'd like clarification none the less.

我特别关注用户控制器

https://stackoverflow.com/Users
https://stackoverflow.com/Users/Login
https://stackoverflow.com/Users/124069/rockinthexstring

我注意到有一个用户"控制器可能带有默认的索引"操作和登录"操作.我面临的问题是可以忽略登录操作,也可以使用UrlParameter.Optional [ID]".

What I'm noticing is that there is a "Users" controller probably with a default "Index" action, and a "Login" action. The problem I am facing is that the login action can be ignored and a "UrlParameter.Optional [ID]" can also be used.

它在 RegisterRoutes 集合中的具体表现如何?还是我遗漏了一些非常明显的东西?

How exactly does this look in the RegisterRoutes collection? Or am I missing something totally obvious?

这是我目前的路线..但它绝对不是正确的.

Here's the route I have currently.. but it's definitely far from right.

    routes.MapRoute( _
        "Default", _
        "{controller}/{id}/{slug}", _
        New With {.controller = "Events", .action = "Index", .id = UrlParameter.Optional, .slug = UrlParameter.Optional} _
    )

推荐答案

可能只是使用特定的路由来处理它,还使用正则表达式来指定 ID 的格式(因此它不会与其他路由混淆将在该位置包含动作名称).

Probably just uses a specific route to handle it, also using a regex to specify the format of the ID (so it doesn't get confused with other routes that would contain action names in that position).

// one route for details
routes.MapRoute("UserProfile",
     "Users/{id}/{slug}",
     new { controller = "Users", action = "Details", slug = string.Empty },
     new { id = @"d+" }
);
// one route for everything else
routes.MapRoute("Default",
     "{controller}/{action}/{id}",
     new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);

这篇关于ASP.NET MVC 像 StackOverflow 中那样的用户路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆