我正在使用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 }
);
}
}
答案 0 :(得分:1)
最后我找到了答案。
正如@StephenMuecke评论
localhost/Customer/Index
与customerDetail
路由匹配(并将"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 }
);
}
}