ASP.NET MVC用户路由一样,在计算器? [英] ASP.NET MVC User routing like that in StackOverflow?

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

问题描述

我看StackOverflow上的路由和我有一个非常noobie的问题,但有些事我想澄清没有少。

我在用户控制器专门看

http://stackoverflow.com/Users 结果
http://stackoverflow.com/Users/Login 结果
<一href=\"http://stackoverflow.com/Users/124069/rockinthesixstring\">http://stackoverflow.com/Users/124069/rockinthesixstring

什么我注意到的是,有一个用户控制器可能与一个默认的指数行动,和一个登录的动作。我现在面临的问题是,登录操作可以忽略和一个UrlParameter.Optional [ID]也可以使用。

为什么说这看在收集的RegisterRoutes?还是我失去了一些东西完全明显?

编辑:这是我目前拥有的路线..但它肯定远远权

  routes.MapRoute(_
        默认,_
        {控制器} / {ID} / {}塞,_
        随着新{.controller =活动,.action =索引,.ID = UrlParameter.Optional,.slug = UrlParameter.Optional} _
    )


解决方案

也许只是使用一个特定的路线来处理它,也用一个正则表达式来指定ID的格式(因此不会混淆与其他的航线将包含在这个位置上动作名)。

  //一个细节航线
routes.MapRoute(用户配置
     用户/ {ID} / {}塞,
     新{控制器=用户,行动=详细信息,蛞蝓=的String.Empty},
     新{ID = @\\ D +}
);
//其他一切一个路由
routes.MapRoute(默认,
     {控制器} / {行动} / {ID}
     新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);

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

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

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.

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

EDIT: 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} _
    )

解决方案

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用户路由一样,在计算器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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