区域的MVC路由问题

时间:2014-07-16 12:07:26

标签: c# asp.net-mvc asp.net-mvc-4

我有一个名为Racing的区域。我已设置路由以使用约束接受参数,如下所示:

全球性的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但现在我无法访问例如Racing / Meeting / HelloWorld而无需传递参数作为Racing / Meeting / HelloWorld / 1。有什么想法吗?

由于

1 个答案:

答案 0 :(得分: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 }
        );


    }
}