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.Mvc;
00006 using System.Web.Routing;
00007 using N2.Engine;
00008
00009 namespace N2.Web.Mvc.Html
00010 {
00011 public static class ContentItemExtensions
00012 {
00013 public static T ResolveService<T>(this HtmlHelper helper) where T : class
00014 {
00015 return helper.ViewContext.RouteData.ResolveService<T>();
00016 }
00017
00018 public static T[] ResolveServices<T>(this HtmlHelper helper) where T : class
00019 {
00020 return helper.ViewContext.RouteData.ResolveServices<T>();
00021 }
00022
00023 public static ContentItem CurrentPage(this HtmlHelper helper)
00024 {
00025 return helper.CurrentPage<ContentItem>();
00026 }
00027 public static T CurrentPage<T>(this HtmlHelper helper) where T : ContentItem
00028 {
00029 if (helper == null) throw new ArgumentNullException("helper");
00030
00031 return helper.ViewContext.CurrentPage<T>();
00032 }
00033
00034 public static ContentItem CurrentItem(this HtmlHelper helper)
00035 {
00036 return helper.CurrentItem<ContentItem>();
00037 }
00038 public static T CurrentItem<T>(this HtmlHelper helper) where T : ContentItem
00039 {
00040 if (helper == null) throw new ArgumentNullException("helper");
00041
00042 return helper.ViewContext.CurrentItem<T>();
00043 }
00044
00045
00046
00047 public static ContentItem CurrentItem(this ViewContext context)
00048 {
00049 return context.CurrentItem<ContentItem>();
00050 }
00051 public static T CurrentItem<T>(this ViewContext context) where T : ContentItem
00052 {
00053 if (context == null) throw new ArgumentNullException("context");
00054
00055 return context.ViewData.CurrentItem<T>()
00056 ?? context.RequestContext.CurrentItem<T>();
00057 }
00058
00059 public static T CurrentPage<T>(this ViewContext context) where T : ContentItem
00060 {
00061 if (context == null) throw new ArgumentNullException("context");
00062
00063 return context.ViewData.CurrentPage<T>()
00064 ?? context.RequestContext.CurrentPage<T>();
00065 }
00066
00067
00068 private static T CurrentItem<T>(this ViewDataDictionary viewData) where T : ContentItem
00069 {
00070 if (viewData == null) throw new ArgumentNullException("viewData");
00071
00072 return viewData.Model as T
00073 ?? viewData[ContentRoute.ContentItemKey] as T;
00074 }
00075 private static T CurrentPage<T>(this ViewDataDictionary viewData) where T : ContentItem
00076 {
00077 if (viewData == null) throw new ArgumentNullException("viewData");
00078
00079 return viewData[ContentRoute.ContentPageKey] as T;
00080 }
00081 }
00082 }