Go to the documentation of this file.00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.Web.Routing;
00006 using N2.Persistence;
00007 using System.Web;
00008 using System.Web.Mvc;
00009 using System.Linq.Expressions;
00010 
00011 namespace N2.Web.Mvc
00012 {
00013         public static class RouteExtensions
00014         {
00015                 public static RouteData ApplyCurrentItem(this RouteData data, string controllerName, string actionName, ContentItem page, ContentItem part)
00016                 {
00017                         data.Values[ContentRoute.ControllerKey] = controllerName;
00018                         data.Values[ContentRoute.ActionKey] = actionName;
00019 
00020                         return data.ApplyContentItem(ContentRoute.ContentPageKey, page)
00021                                 .ApplyContentItem(ContentRoute.ContentPartKey, part);
00022                 }
00023 
00024                 internal static RouteData ApplyContentItem(this RouteData data, string key, ContentItem item)
00025                 {
00026                         if (data == null) throw new ArgumentNullException("data");
00027                         if (key == null) throw new ArgumentNullException("key");
00028 
00029                         if (item != null)
00030                         {
00031                                 data.DataTokens[key] = item;
00032                                 data.Values[key] = item.ID;
00033                         }
00034 
00035                         return data;
00036                 }
00037 
00038                 public static ContentItem CurrentItem(this RouteData routeData)
00039                 {
00040                         return routeData.DataTokens.CurrentItem(ContentRoute.ContentPartKey)
00041                                 ?? routeData.DataTokens.CurrentItem(ContentRoute.ContentPageKey);
00042                 }
00043 
00044                 public static ContentItem CurrentPage(this RouteData routeData)
00045                 {
00046                         return routeData.DataTokens.CurrentItem(ContentRoute.ContentPageKey);
00047                 }
00048 
00049                 internal static ContentItem CurrentItem(this RouteValueDictionary data, string key)
00050                 {
00051                         if (data.ContainsKey(key))
00052                                 return data[key] as ContentItem;
00053                         return null;
00054                 }
00055 
00056                 public static T CurrentItem<T>(this RouteValueDictionary data, string key, IPersister persister)
00057                         where T: ContentItem
00058                 {
00059                         if (data.ContainsKey(key))
00060                         {
00061                                 object value = data[key];
00062                                 if (value == null)
00063                                         return null;
00064                                 
00065                                 var item = value as T;
00066                                 if (item != null)
00067                                         return item as T;
00068 
00069                                 if(value is int)
00070                                         return persister.Get((int)value) as T;
00071 
00072                                 int itemId;
00073                                 if (value is string && int.TryParse(value as string, out itemId))
00074                                         return persister.Get((int)value) as T;
00075                         }
00076 
00077                         return null;
00078                 }
00079         }
00080 }