MVC 中的路由找不到新的 url

Routing in MVC not finding the new url

我不是很懂这个概念,所以做了一个手册,看了一些文章,我决定问问大家。

我想更改,只是为了测试,来自:

localhost/Home/List

收件人:

localhost/Custom/List

所以我的:

RouteConfig.cs

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("Custom", "Custom/List/",
        new
        {
            Controller = "Home",
            Action = "List"
        });

}

但是它不起作用。第一个 url 仍在工作,但第二个没有找到任何东西。

谢谢

路由按顺序匹配,您的 Default 路由匹配任何 url 具有 0 到 3 个分段,因此 ../Custom/List 调用 List() 方法 CustomController.

您需要更改路线的顺序,使 CustomDefaultRoute 之前。 ../Custom/List 将首先匹配该路由并转到 HomeController

List() 方法