asp.net - 如果我在 RouteConfig 中更改它的顺序,为什么这条路线不起作用?
问题描述
当我访问这条路线http://localhost:60015/myroute
时,如果路线的顺序如下所示,它将不起作用:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"myroute",
"myroute/{name}",
new { controller = "myroute", action = "search", name = "" }
);
}
如果我颠倒顺序,它会起作用。这意味着什么?这是怎么发生的?
解决方案
这是因为您添加到路由表的路由顺序很重要。您的新自定义路由应添加到现有默认路由之前。如果您颠倒了顺序,那么将始终调用默认路由而不是自定义路由。
推荐阅读
- php - 无法从 Ionic 4 中的本地服务器获取 JSON 请求
- sql - 将存储在字符串变量中的 CSV 转换为表
- webpack - webpack babel 前置 IE 11
- prolog - 向 DCG 添加解析约束
- twig - 模板中的木材 get_theme_mod
- scala - Scala function composition totalFn(partialFn(totalFn(x)))
- php - Updated to PHP 7.2 on RHEL and getting frequent cURL errors 28: timed out before SSL handshake
- ethernet - access two devices on different subnets
- ruby - Why does uniq return original value of array after map in Ruby?
- scala - Scala is not looping through the for loop