C# 区域的MVC路由问题
我有一个领域叫赛车。我已使用以下约束设置管线以接受参数: 全球asax:C# 区域的MVC路由问题,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我有一个领域叫赛车。我已使用以下约束设置管线以接受参数: 全球asax: protected void Application_Start() { //AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilte
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,但现在我无法访问例如Racing/Meeting/HelloWorld,而不必将参数传递为Racing/Meeting/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 }
);
}
}
您能否显示您的应用程序\u start from global.asax,您注册路由和区域路由的顺序很重要。您是否尝试翻转路由定义。i、 确定你的身份证路线,第一条?是的,我试过翻转。有关完整代码,请参见编辑。除了Meeting\Racecards之外,还有其他方法采用日期参数吗?更具体地说,您是否需要像它一样通用的路线?能否在url中明确定义会议卡和比赛卡?这当然可以防止路由混乱,但对您的用例来说可能太有限了。如果我将URL更改为URL:“Racing/Meeting/Racecards/{date},那么这仍然不允许我在未传递参数的情况下访问正常的控制器/操作?