在ASP.NET MVC中,路由不再传递约束的问题可能是由于路由配置不正确导致的。以下是一种可能的解决方法:
protected void Application_Start()
{
// ...
RouteConfig.RegisterRoutes(RouteTable.Routes);
// ...
}
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 },
constraints: new { id = @"\d+" } // 此处定义了id参数的约束
);
}
上述示例中,约束定义了id参数只能是数字。您可以根据您的需求定义适当的约束。
public ActionResult Details(int id)
{
// ...
}
在上述示例中,id参数使用了一个int类型的约束,表示id只能是整数。您可以根据您的需求定义适当的约束。
通过检查和修改路由配置,您应该能够解决ASP.NET MVC中路由不再传递约束的问题。
下一篇:Asp.net MVC路由配置