ASP.NET路由多控制器

时间:2018-11-25 11:07:46

标签: c# asp.net-mvc asp.net-mvc-routing

我要在下面路由网址

  

url /类别代码,例如:url / luxury; url / normal; url /轿车

并发布详细信息

  

url / post-detail-code ex:url / honda-civic-20-review

和品牌网址

  

网址/品牌代码,例如:网址/丰田; url / hyndai; url / lexus

这是路由配置

routes.MapRoute(
   "postdetail",
   "{SeoFriendlyUrl}",
   defaults: new { controller = "PostDetail", action = "Index", SeoFriendlyUrl = UrlParameter.Optional },
   namespaces: new[] { "RiviWeb.Controllers" }
);

routes.MapRoute(
   "postcategory",
   "{SeoFriendlyUrl}",
   new { controller = "PostCategory", action = "Index",SeoFriendlyUrl = UrlParameter.Optional  },
   namespaces: new[] { "RiviWeb.Controllers" }
);



routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}",
    defaults: new { controller = "Home", action = "Index"},
    namespaces: new[] { "RiviWeb.Controllers" }
);

1 个答案:

答案 0 :(得分:0)

路由模板必须唯一以避免路由冲突

//url/details/honda-civic-20-review
routes.MapRoute(
   name: "postdetail",
   url: "details/{SeoFriendlyUrl}",
   defaults: new { controller = "PostDetail", action = "Index", SeoFriendlyUrl = UrlParameter.Optional },
   namespaces: new[] { "RiviWeb.Controllers" }
);

//url/categories/luxury
//url/categories/normal
//url/categories/sedan    
routes.MapRoute(
   name: "postcategory",
   url: "categories/{SeoFriendlyUrl}",
   defaults: new { controller = "PostCategory", action = "Index",SeoFriendlyUrl = UrlParameter.Optional  },
   namespaces: new[] { "RiviWeb.Controllers" }
);

//url/brands/toyota
//url/brands/hyndai
//url/brands/lexus    
routes.MapRoute(
   name: "brand",
   url: "brands/{SeoFriendlyUrl}",
   defaults: new { controller = "Brands", action = "Index",SeoFriendlyUrl = UrlParameter.Optional  },
   namespaces: new[] { "RiviWeb.Controllers" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}",
    defaults: new { controller = "Home", action = "Index"},
    namespaces: new[] { "RiviWeb.Controllers" }
);