C# 在.NET MVC中为一个控制器定义多个静态路由和一个动态路由
我定义了几个静态路由,如下所示:C# 在.NET MVC中为一个控制器定义多个静态路由和一个动态路由,c#,asp.net-mvc,asp.net-mvc-5,routing,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc 5,Routing,Asp.net Mvc Routing,我定义了几个静态路由,如下所示: routes.MapRoute( name: "LogOutRoute", url: "Index/LogOut", defaults: new { controller = "Index", action = "LogOut" } ); routes.MapRoute( name: "Tutorials",
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
http://localhost:60617/Index/12345/
第三个是动态路径,如下所示:
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
http://localhost:60617/Index/12345/
我想为我的索引控制器定义这两个静态路由:
/Index/Tutorials
/Index/LogOut
其他路线应指向:
/Index/{id}
我现在定义它的方式适用于2个静态路由,但当我尝试传递这样的参数时,它不是以下两个静态路由之一:
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
http://localhost:60617/Index/12345/
其中12345是ID,我得到以下错误:
The resource cannot be found.
如何正确定义这些路线?有人能帮我吗
以下是路线类别:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
routes.MapRoute(
name: "ResetPwdRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "ResetPwd", id = UrlParameter.Optional }
);
}
}
绘制路线的顺序很重要 通用路由应在更具体的路由之后映射,以避免路由冲突
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
routes.MapRoute(
name: "ResetPwdRoute",
url: "User/ResetPwd/{id}",
defaults: new { controller = "User", action = "ResetPwd", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
还应该注意的是,在这种情况下,
resetpDroote
完全没有意义,因为Default
路由无论如何都会处理这种情况。