2012-12-21 11 views
5

के लिए काम नहीं कर रहा है मार्ग http://localhost:2222/2012-adidas-spring-classic/37 क्यों रूट मार्ग से उठाया नहीं जाएगा? मुझे 404 त्रुटि मिलती है।रूट प्रतिबंध एएसपी.नेट एमवीसी

 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 के साथ काम नहीं करेगा –

संबंधित मुद्दे