另一个MVC路由问题
本文关键字:问题 路由 MVC 另一个 | 更新日期: 2023-09-27 17:58:16
总有一天我会理解路由,但这就是我所拥有的:
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
);
}
如果我去http://localhost一切正常
如果我去http://localhost/Home/Index/1234一切正常
如果我去http://localhost/1234它是404的
我尝试了Phil Haack的路由调试器,但因为它抛出了404,所以路由调试器不起作用。
我在RegisterRoutes中要做什么http://localhost/1234工作
routes.MapRoute(
"LicenceCode",
"{LicenceCode}"
new { controller = "Home", action = "Index", LicenceCode = UrlParameter.Optional } // Parameter defaults
);
然后/1234
将路由到Home
控制器的Index
动作:
public ActionResult Index(string licenceCode)
{
...
}
您必须使用以下根而不是您的根:
routes.MapRoute(
"Default", // Route name
"{LicenceCode}", // URL with parameters
new { controller = "Home", action = "Index", LicenceCode = UrlParameter.Optional } // Parameter defaults
);
我面前没有Visual Studio,但我认为它会是
routes.MapRoute(
"Default2",
"{LicenceCode}",
new { controller = "Home", action = "Index", LicenceCode = UrlParameter.Optional }
);
你肯定想把这个放在你的路线注册的最后一个,因为我想这会劫持很多路径。
如上所述设置默认路由路线。地图路线("默认",//路由名称"{LicenseCode}",//带参数的URLnew{controller="Home",action="Index",LicenseCode=UrlParameter.可选}//参数默认值);
但是不要在全局中添加1000行路由。我在一些MVC 1站点上看到过这样做,维护起来真的很糟糕。
对于其他管线,请通过视图和控制器对其进行处理。示例:
在控制器中:在ActionResults方法中,您可以return RedirectToAction("ClientEnrollment"、"Cis");
在视图中:具有链接@Html.ActionLink("Select","ClientDetails","Cis",new{id=item.ClientId},null)|