2011-11-16 54 views
0

我正在研究围绕体育赛事的应用程序。有足球比赛和网球比赛等不同类型的赛事。根据比赛的类型,我希望由另一个区域处理请求。但事件及其比赛类型可由应用程序的用户配置并存储在数据库中。基于路线参数值的asp.net mvc动态区域选择

Currrently我有概念的证明了这一点:

public class SoccerTournamentAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get 
     { 
      return "SoccerTournament"; 
     } 
    } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 
     var soccerTournaments = new string[] { "championsleague", "worldcup" }; 
     foreach (var tournament in soccerTournaments) 
     { 
      context.MapRoute(
       string.Format("SoccerTournament_default{0}", tournament), 
       string.Format("{0}/{{controller}}/{{action}}/{{id}}", tournament), 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
       new[] { "Mvc3AreaTest1.Areas.SoccerTournament.Controllers" } 
       ); 
     } 
    } 
} 

,我想soccerTournaments来自数据库(不是问题),它仅适用,但我也希望它的工作很快问作为一个新的事件/比赛类型记录被添加到数据库中,并且在这种情况下不起作用。

如何使区域选择动态而不是硬编码到路线中?

回答

1

区域注册只发生在应用程序启动时,所以启动后添加的任何锦标赛都不会被捕获,直到重新启动。

要为您的锦标赛提供动态路线方案,您必须重新定义您的地区路线并添加RouteConstraint

重新定义您的路线如下:

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     "SoccerTournament_default", 
     "{tournament}/{controller}/{action}/{id}", 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
     new { tournament = new MustBeTournamentName() }, 
     new string[] { "Mvc3AreaTest1.Areas.SoccerTournament.Controllers" } 
    ); 
} 

比,你可以创建MustBeTournamentName RouteConstraint是在回答这个问题类似于RouteConstraint:Asp.Net Custom Routing and custom routing and add category before controller