首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC区域路由问题

MVC区域路由问题
EN

Stack Overflow用户
提问于 2014-07-16 12:07:26
回答 1查看 51关注 0票数 0

我有一个叫赛车的区域。我已经设置了使用约束接受参数的路由,如下所示:

全局asax:

代码语言:javascript
复制
    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

代码语言:javascript
复制
 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();


    }
}

赛车区注册

代码语言:javascript
复制
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/1,就可以访问Racing/Meeting/HelloWorld。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-16 12:22:19

您的区域注册需要在您的默认路径之前完成。尝试将它们移动到方法的顶部。

代码语言:javascript
复制
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 }
        );


    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24780402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档