ASP MVC制作自定义路线

时间:2017-11-02 19:29:38

标签: asp.net-mvc

我有几个自定义路由,使我的应用程序在控制器之前设置用户的用户名。而且我想在用户名后显示一些控制器:

例如:

localhost:44303/johndoe //it should show profile > index
localhost:44303/johndoe/Profile
localhost:44303/johndoe/Profile/Index
localhost:44303/johndoe/Profile/Setting
localhost:44303/johndoe/Activity
localhost:44303/johndoe/Activity/Index
localhost:44303/johndoe/Activity/Setting

这是我的路线配置:

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

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

  /*********** PROJECT OTHER ROUTE****************/

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

        routes.MapRoute(
           name: "Default1",
           url: "{controller}/{action}/{parametr1}/{parametr2}/{parametr3}",
           defaults: new { controller = "Home", action = "Index", parametr1 = "1", parametr2 = UrlParameter.Optional, parametr3 = UrlParameter.Optional }
       );

在这种情况下,除此之外,所有网址都正常工作:     本地主机:44303 /输入johndoe

我收到The resource cannot be found.错误。

我添加了下面的路线,但新问题出现在其他路线中,如localhost:44303

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

我该如何解决这个问题?我的主要问题是一些网址,如localhost:44303/johndoe

1 个答案:

答案 0 :(得分:0)

它应该是通过并且达到默认路线。如果您希望它转到个人资料/索引,请设置默认方式