简体   繁体   English

ASP.NET MVC路由-映射操作而不是控制器

[英]asp.net mvc routing - map actions not controllers

I know it's more of the same (SO has more than 5,600 questions on this), but I've been sitting on this one for a couple of days now so I guess it was the right time to ask a question. 我知道这差不多 (SO对此有5600多个问题 ),但是我已经坐在这个问题上几天了,所以我想是时候提出问题了。

My requirements 我的要求

I want to have the following routes in my asp.net mvc app: 我想在asp.net mvc应用程序中使用以下路由:

  1. myapp.com/sigin -> Controller = Account, Action = SignIn myapp.com/sigin->控制器=帐户,操作=登录
  2. myapp.com/signout -> Controller = Account, Action = SignOut myapp.com/signout->控制器=帐户,操作=注销
  3. myapp.com/joe.smith -> Controller = User, Action = Index myapp.com/joe.smith->控制器=用户,操作=索引
  4. myapp.com/joe.smith/following -> Controller = User, Action = Following myapp.com/joe.smith/关注->控制器=用户,操作=关注
  5. myapp.com/joe.smith/tracks -> Controller = Tracks, Action = Index myapp.com/joe.smith/tracks->控制器=曲目,操作=索引
  6. myapp.com/about -> Controller = About, Action = Index myapp.com/about->控制器=关于,操作=索引
  7. Any other default route, so that's why I left the standard one there. 任何其他默认路由,这就是为什么我在此处保留标准路由的原因。

My Code 我的密码

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "About",
            url: "about",
            defaults: new { controller = "About", action = "Index" }
        );

        routes.MapRoute(
            name: "MyTracks",
            url: "{username}/tracks",
            defaults: new { controller = "MyTracks", action = "Index" }
        );

        routes.MapRoute(
            name: "MyTunes",
            url: "{username}/tunes",
            defaults: new { controller = "MyTunes", action = "Index" }
        );

        routes.MapRoute(
            name: "MyProfile",
            url: "{username}",
            defaults: new { controller = "User", action = "Index"},
            constraints: new { username = "" }
         );

        routes.MapRoute(
            name: "Account",
            url: "{action}",
            defaults: new { controller = "Account" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

Issue 问题

Routes number 3 and 4 just don't work as they get mixed up with route 1 and 2. I have tried debugging my code with Phil Haack's routing debugger , but no luck. 路由3和4根本不起作用,因为它们与路由1和2混淆了。我曾尝试使用Phil Haack的路由调试器调试代码 ,但是没有运气。 Any ideas what I am doing wrong? 有什么想法我做错了吗?

The problem is in your last two custom routes. 问题出在您的最后两个自定义路线中。 All the routing framework has to work with is just a single token from the URL, and two possible routes to match it with. 所有路由框架必须使用的只是URL中的单个令牌,以及两个与其匹配的可能路由。 For example, if you attempt to go to the URL, /signin , how is the routing framework supposed to know there's not a user with username "signin". 例如,如果您尝试访问URL /signin ,那么路由框架应该如何知道没有用户名为“ signin”的用户。 It's obvious to you, a human, what should happen, but a machine can only do so much. 对于人类来说,这很明显,应该发生什么,但是机器只能做很多事情。

You need to differentiate the routes in some way. 您需要以某种方式区分路线。 For example, you could do u/{username} . 例如,您可以执行u/{username} That would be enough to help the routing framework out. 这足以帮助路由框架。 Short of that, you'll need to define custom routes for each account action before the user route. 简而言之,您需要在用户路由之前为每个帐户操作定义自定义路由。 For example: 例如:

routes.MapRoute(
    name: "AccountSignin",
    url: "signin",
    defaults: new { controller = "Account", action = "Signin" }
);

// etc.

routes.MapRoute(
    name: "MyProfile",
    url: "{username}",
    defaults: new { controller = "User", action = "Index"},
    constraints: new { username = "" }
 );

That way, any of the actual action names will match first. 这样,任何实际的动作名称都将首先匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM