2012-12-21 3 views
5

경로 http://localhost:2222/2012-adidas-spring-classic/37이 아래 경로에서 선택되지 않는 이유는 무엇입니까? 404 오류가 발생합니다.ASP.NET MVC에서 경로 제한이 작동하지 않습니다.

 public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      routes.IgnoreRoute("{*vti_inf}", new { vti_inf = @"(.*/)?_vti_inf.html(/.*)?" }); 
      routes.IgnoreRoute("{*vti_rpc}", new { vti_rpc = @"(.*/)?_vti_rpc(/.*)?" }); 

      #region API 

      routes.MapRouteLowercase(
      "NamedHomeEvent", 
      "{year}-{name}/{Id}", 
      new { controller = "Event", action = "Index", year = DateTime.Now.Year }, 
      new { year = @"\d{4}", Id = @"\d+" } 
      ); 



    public virtual ActionResult Index(int? id, int? year, string name) 
     { 
+0

당신은 또한 사용할 수 있습니다 http://getglimpse.com/ 디버그 경로에을 (그리고 더 많이) (http://getglimpse.com/Help/Plugin/Routes) – robasta

답변

4

라우팅 엔진이 여기에 도움을 줄 수 없습니다. 이 경우 처리하는 사용자 정의 경로를 작성할 수

public class MyRoute : Route 
{ 
    public MyRoute() 
     : base(
      "{year-name}/{id}", 
      new RouteValueDictionary(new { controller = "Event", action = "Index", id = UrlParameter.Optional }), 
      new RouteValueDictionary(new { id = @"\d*" }), 
      new MvcRouteHandler() 
     ) 
    { 
    } 

    public override RouteData GetRouteData(HttpContextBase httpContext) 
    { 
     var routeData = base.GetRouteData(httpContext); 
     if (routeData == null) 
     { 
      return null; 
     } 

     var yearName = (string)routeData.Values["year-name"]; 
     if (string.IsNullOrWhiteSpace(yearName)) 
     { 
      return null; 
     } 

     var parts = yearName.Split(new[] { '-' }, 2, StringSplitOptions.RemoveEmptyEntries); 
     if (parts.Length < 2) 
     { 
      return null; 
     } 

     var year = parts.First(); 
     int yearValue; 
     if (!int.TryParse(year, out yearValue)) 
     { 
      return null; 
     } 

     var name = parts.Last(); 

     routeData.Values.Add("year", year); 
     routeData.Values.Add("name", name); 

     return routeData; 
    } 
} 

을하고이 경로 등록 :

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.IgnoreRoute("{*vti_inf}", new { vti_inf = @"(.*/)?_vti_inf.html(/.*)?" }); 
    routes.IgnoreRoute("{*vti_rpc}", new { vti_rpc = @"(.*/)?_vti_rpc(/.*)?" }); 

    routes.Add("NamedHomeEvent", new MyRoute()); 
} 
+0

나는 이것이 T4MVC에서만 작동하지 않을 것이라고 믿는다. –

관련 문제