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

问题描述

我查看了 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.

我正在专门研究用户控制器

I'm looking specifically at the Users controller

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

我注意到的是,有一个用户"控制器可能带有一个默认的索引"操作和一个登录"操作.我面临的问题是可以忽略登录操作,也可以使用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}
);

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

07-30 22:46