2011-09-27 94 views
1

我試着做這樣的事情:MVC與動態路由3路由幫助

routes.MapRoute("Product", "{product}/{id}", 
          new 
           { 
            action = "Product", 
            controller = "Home", 
            product = UrlParameter.Optional, 
            id = UrlParameter.Optional 
           }); 

它給了我錯誤時,我嘗試加載404頁,我認爲, 我試着讓URL看起來像這樣: www.tables.com/productName/ID。 我怎樣才能做到這一點無需添加一個強類型的話是這樣的:路由的

routes.MapRoute("Product", "Products/{product}/{id}", ...) 

休息:

routes.MapRoute("Product", "{product}/{id}", 
          new 
           { 
            action = "Product", 
            controller = "Home", 
            product = UrlParameter.Optional, 
            id = UrlParameter.Optional 
           }); 

      routes.MapRoute("Category", "Category/{category}/{template}", 
          new 
          { 
           action = "Index", 
           controller = "Category", 
           category = UrlParameter.Optional, 
           template = UrlParameter.Optional 
          }); 

      routes.MapRoute("Profile", "Profile/{fullName}", 
          new 
          { 
           action = "Index", 
           controller = "Profile", 
           fullName = UrlParameter.Optional 
          }); 


      routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 

感謝。

+0

這可能與另一條路線發生衝突。你可以發佈剩下的映射路線嗎(按照正確的順序) –

回答

1

您的問題是產品路線將匹配不以類別或配置文件開頭的所有內容。

我會將產品路線放置在默認路線之前,並使用IRouteConstraint以使其與非產品不匹配。

代碼示例:

routes.MapRoute("Category", "Category/{category}/{template}", 
       new 
       { 
        action = "Index", 
        controller = "Category", 
        category = UrlParameter.Optional, 
        template = UrlParameter.Optional 
       }); 

routes.MapRoute("Profile", "Profile/{fullName}", 
       new 
       { 
        action = "Index", 
        controller = "Profile", 
        fullName = UrlParameter.Optional 
       }); 


routes.MapRoute("Product", "{product}/{id}", 
       new 
        { 
         action = "Product", 
         controller = "Home", 
         product = UrlParameter.Optional, 
         id = UrlParameter.Optional 
        }, 
       new { product = new ProductRouteConstraint() }); 

routes.MapRoute(
    "Default", // Route name 
    "{controller}/{action}/{id}", // URL with parameters 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
); 

和路由約束:

public class ProductRouteConstraint : IRouteConstraint 
    { 
     public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) 
     { 
      if (routeDirection == RouteDirection.IncomingRequest && 
       parameterName.ToLowerInvariant() == "product") 
      { 
       var productName = values[parameterName] as string; 
       if (productName == null) 
        return false; 

       var productId = values["id"] as string; 
       if (productId == null) 
        returns false; 

       return ProductCatalogue.HasProductById(productId); 
      } 

      return false; 
     } 
    } 

的器產品目錄顯然應該有,但是你查找的產品在你的系統所取代。