C# ASP.NET MVC 5 Url重写以包含控制器中的所有操作?

C# ASP.NET MVC 5 Url重写以包含控制器中的所有操作?,c#,asp.net,asp.net-mvc,url-rewriting,routes,C#,Asp.net,Asp.net Mvc,Url Rewriting,Routes,我想知道是否有可能在Route.Config文件中编写一条规则,该规则可以包含单个控制器中的所有操作?我已经读过了,但它有点超出我的理解(我刚刚开始学习url重写和路由,术语并不熟悉) 我已设法将我的一项操作从mydomain.co.za/培训生/Action?id=123更改为mydomain.co.za/培训生/Action/123,但我希望能够包含培训生控制器中的所有操作,这样,您就可以有一个规则来生成实习生/Action1/123或实习生/Action2/123这就是我使用的代码:

我想知道是否有可能在Route.Config文件中编写一条规则,该规则可以包含单个控制器中的所有操作?我已经读过了,但它有点超出我的理解(我刚刚开始学习url重写和路由,术语并不熟悉)

我已设法将我的一项操作从
mydomain.co.za/培训生/Action?id=123
更改为
mydomain.co.za/培训生/Action/123
,但我希望能够包含培训生控制器中的所有操作,这样,您就可以有一个规则来生成
实习生/Action1/123
实习生/Action2/123
这就是我使用的代码:

        routes.MapRoute(
            name: "ActionRewrite",
            url: "Trainee/Action/{id}",
            defaults: new { controller = "Trainee", action = "Action" }
        );
另一方面,是否可以在URL中隐藏参数,这样无论用户在做什么,都可以使用mydomain.co.za/Action/

routes.MapRoute(
            name: "ActionRewrite",
            url: "Trainee/{action}/{id}",
            defaults: new { controller = "Trainee", action = {action} }
        );
  • 请记住,您可以在url匹配中使用通配符

  • 如果需要,可以指定{id}参数仍然是可选的,这意味着它还将匹配不指定参数的操作

  • 试试这个。。 使用自定义id和名称重新连接Url。 加载项应用程序启动路径配置。 public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes();

            routes.MapRoute(
              name: "contactus",
              url: "contactus",
              defaults: new { Controller = "Cms", action = "Index", id = (int)Common.CMSContactUs },
               namespaces: new[] { "QZero.Controllers" }
          );
            routes.MapRoute(
               name: "aboutus",
               url: "aboutus",
               defaults: new { Controller = "Cms", action = "Index", id = (int)Common.CMSAboutUs },
                namespaces: new[] { "QZero.Controllers" }
           );
            routes.MapRoute(
           name: "useragreement",
           url: "useragreement",
           defaults: new { Controller = "Cms", action = "Index", id = (int)Common.CMSUserAgreement },
            namespaces: new[] { "QZero.Controllers" }
       );
            routes.MapRoute(
               name: "Default",
               url: "{controller}/{action}/{id}",
               defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
               namespaces: new[] { "QZero.Controllers" }
           );
    
        }
    }
    

    我会把旁注作为第二个问题贴出来,这似乎起到了作用!我从来都不知道您可以将您的操作指定为“参数”。谢谢你。@barnacle.m是的,它很灵活。对于真正的自定义路由,您可以看看如何实现自定义控制器工厂,在那里您可以访问路由数据收集。周围有很多例子。很高兴它起作用了