区域的MVC路由问题
本文关键字:问题 路由 MVC 区域 | 更新日期: 2023-09-27 18:13:20
我有一个区域叫赛车。我已经设置了路由来接受使用如下约束的参数:
全球asax:
protected void Application_Start()
{
//AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
Route.config
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 = "Home", action = "Index", id = UrlParameter.Optional }
);
AreaRegistration.RegisterAllAreas();
}
}
赛区注册
public class RacingAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Racing";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
// this maps to Racing/Meeting/Racecards/2014-01-06 and WORKS!!
context.MapRoute(
name: "Racecard",
url: "Racing/{controller}/{action}/{date}",
defaults: new { controller="Meeting", action = "Racecards", date = UrlParameter.Optional },
constraints: new { date = @"^'d{4}$|^'d{4}-((0?'d)|(1[012]))-(((0?|[12])'d)|3[01])$" }
);
// this maps to Racing/Meeting/View/109 and WORKS!!
context.MapRoute(
"Racing_default",
"Racing/{controller}/{action}/{id}",
defaults: new { controller="Meeting", action = "Hello", id = UrlParameter.Optional }
);
}
}
上述两个工作的URL的指定,但现在我不能访问例如赛车/会议/HelloWorld没有传递参数为赛车/会议/HelloWorld/1。什么好主意吗?
谢谢
您的区域注册需要在默认路由之前完成。尝试将它们移到方法
的顶部public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
AreaRegistration.RegisterAllAreas();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}