使用RouteConfig更改路径后,旧路径不起作用

时间:2017-09-11 06:38:01

标签: c# asp.net-mvc

我正在使用Entity Framework开发一个C#ASP.NET MVC项目。

我正在尝试使用RouteConfig.cs

更改网址路径

首先,代码看起来像这样

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
        );
    }
}

我想将网址localhost/Customer/Index变为localhost/Index

所以我添加了这行代码并且工作正常

routes.MapRoute(
    name: "customer",
    url: "customer",
    defaults: new { controller = "Customer", action = "Index"}
);

接下来,我尝试将localhost/Customer/Details/2更改为localhost/CustomerName/2

// CustomerName varies with each customer

所以我添加了这个

 routes.MapRoute(
     name: "customerDetail",
     url: "customer/{name}/{id}",
     defaults: new { controller = "Customer", action = "Details", id = UrlParameter.Optional, name = UrlParameter.Optional }
);

现在客户详细信息页面正确加载。

但是使用此路径localhost/Customer/Index加载索引页会导致错误,而localhost/Customer不会导致错误。为什么会这样?

修改

这就是我的RouteConfig看起来像

的方式
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "customer",
            url: "customer",
            defaults: new { controller = "Customer", action = "Index"}
        );
        routes.MapRoute(
            name: "customerDetail",
            url: "customer/{name}/{id}",
            defaults: new { controller = "Customer", action = "Details", id = UrlParameter.Optional, name = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
        );
    }
}

2 个答案:

答案 0 :(得分:1)

最后我找到了答案。

  

正如@StephenMuecke评论

     
    

localhost/Customer/IndexcustomerDetail路由匹配(并将"Index"传递给id参数)

  

所以我重新检查了我的代码,发现id中的localhost/Customer/Details/2字段不是可选的,我意外地将其定义为可选,id = UrlParameter.Optional

 routes.MapRoute(
        name: "customerDetail",
        url: "customer/{name}/{id}",
        defaults: new { controller = "Customer", action = "Details", name = UrlParameter.Optional }
    );

完整的RoutConfig代码:

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

        routes.MapRoute(
            name: "customer",
            url: "customer",
            defaults: new { controller = "Customer", action = "Index"}
        );
        routes.MapRoute(
            name: "customerDetail",
            url: "customer/{name}/{id}",
            defaults: new { controller = "Customer", action = "Details",name = UrlParameter.Optional }
    );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
        );

    }
}

答案 1 :(得分:0)

尝试以下代码。从现有代码中删除id UrlParameter.Optional

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

    routes.MapRoute(
        name: "customer",
        url: "customer",
        defaults: new { controller = "Customer", action = "Index"}
    );
    routes.MapRoute(
        name: "customerDetail",
        url: "customer/{name}/{id}",
        defaults: new { controller = "Customer", action = "Details", name = UrlParameter.Optional } // Removed idas optional from existing code
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
    );

   }
}