Orchard路由
Orchard模塊開發中,每個模塊都相當於MVC中的Area,默認的路由便是{模塊名稱}/{Controller}/{Action},很多時候這個Url不符合我們需求,這時候就需要自定路由了。Orchard提供了自定路由的方法,便是實現IRouteProvider。例子如下:
public class Routes : IRouteProvider
{
public void GetRoutes(ICollection<RouteDescriptor> routes)
{
foreach (var routeDescriptor in GetRoutes())
routes.Add(routeDescriptor);
}
public IEnumerable<RouteDescriptor> GetRoutes()
{
return new[]
{
new RouteDescriptor {
Priority = 99,//優先級,越高越優先匹配
Route = new Route(
"Admin/Article", // 自定義路由的Url模板,格式同MVC路由格式
new RouteValueDictionary {
{"area", "JiaZheng"}, // Area名稱就是模塊名
{"controller", "Article"},
{"action", "Index"}
},
new RouteValueDictionary(),
new RouteValueDictionary {
{"area", "JiaZheng"} // Area名稱就是模塊名
},
new MvcRouteHandler())
}
};
}
}
雖然實現了自定義路由,但是很不方便。經過測試,MVC自帶的RouteAttribute是沒有效果的,但是可以用反射來實現相同的效果。Url.Action也不一定能匹配到最終的Url,如果要獲取最終顯示的Url則用Url.AbsoluteAction這個Orchard的拓展。