C# 另一个MVC路由问题

C# 另一个MVC路由问题,c#,asp.net,asp.net-mvc,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,总有一天我会明白路线,但这就是我所拥有的: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("favicon.ico"); routes.MapRoute( "Default",

总有一天我会明白路线,但这就是我所拥有的:

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



            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{LicenceCode}", // URL with parameters
                new { controller = "Home", action = "Index", LicenceCode = UrlParameter.Optional } // Parameter defaults
            );

        }
如果我去的话一切都好

如果我去的话一切都好

如果我去404的

我尝试了Phil Haack的路由调试器,但因为它抛出了404,路由调试器无法工作

我在登记处要做什么才能工作

然后
/1234
将路由到
控制器的
索引
操作:

public ActionResult Index(string licenceCode)
{
    ...
}

我前面没有VisualStudio,但我想应该是

routes.MapRoute(
    "Default2",
    "{LicenceCode}",
    new { controller = "Home", action = "Index", LicenceCode = UrlParameter.Optional }
);

你肯定会想把最后一个放在你的路线注册中,因为我想这会劫持很多路径。

你必须使用下面的根而不是你的根:

routes.MapRoute(
    "Default", // Route name
    "{LicenceCode}", // URL with parameters
    new { controller = "Home", action = "Index", LicenceCode = UrlParameter.Optional } // Parameter defaults
);

如上所述设置默认路由 routes.MapRoute( “默认值”,//路由名称 “{LicenseCode}”,//带参数的URL 新建{controller=“Home”,action=“Index”,LicenceCode=UrlParameter.Optional}//参数默认值 );

但是不要在global.asax中添加1000条线路。我在几个MVC 1站点上看到过这样做,维护起来真的很糟糕

对于其他管线,请通过视图和控制器处理它们。例如:

  • 在控制器中:在ActionResults方法中,您可以 返回重定向操作(“客户集合”、“CI”)

  • 在视图中:具有链接 @ActionLink(“Select”、“ClientDetails”、“Cis”、new{id=item.ClientId},null)|


  • 这是我目前的路线之外的路线吗?我有其他控制器,所以我需要能够转到example@Jon,你不能。您希望路由引擎如何在
    /foo
    /basket
    之间消除歧义,在第一种情况下
    foo
    表示许可证代码,在第二种情况下表示控制器?这根本没有道理。例如,如果您的许可证号码遵循某种模式,您可以在
    MapRoute
    的第四个参数中指定正则表达式路由约束。通过这种方式,您可以在默认路由之前设置许可证路由,并且如果满足约束,许可证路由将匹配/
    routes.MapRoute(
        "Default", // Route name
        "{LicenceCode}", // URL with parameters
        new { controller = "Home", action = "Index", LicenceCode = UrlParameter.Optional } // Parameter defaults
    );