Go to the documentation of this file.00001 using System;
00002 using System.Linq;
00003 using System.Web.Routing;
00004 using N2.Persistence;
00005 using N2.Engine;
00006 using System.Collections.Generic;
00007
00008 namespace N2.Web.Mvc
00009 {
00010 public static class RequestContextExtensions
00011 {
00012 public static string ToQueryString<K,V>(this IDictionary<K,V> values)
00013 {
00014 if (values == null)
00015 return null;
00016 return string.Join("&", values.Select(kvp => kvp.Key + "=" + kvp.Value).ToArray());
00017 }
00018
00019 public static ContentItem CurrentItem(this RequestContext context)
00020 {
00021 return context.CurrentItem<ContentItem>();
00022 }
00023 public static T CurrentPart<T>(this RequestContext context) where T : ContentItem
00024 {
00025 return context.CurrentItem<T>(ContentRoute.ContentPartKey);
00026 }
00027 public static T CurrentItem<T>(this RequestContext context) where T : ContentItem
00028 {
00029 return context.CurrentItem<T>(ContentRoute.ContentItemKey)
00030 ?? context.CurrentItem<T>(ContentRoute.ContentPartKey)
00031 ?? context.CurrentItem<T>(ContentRoute.ContentPageKey);
00032 }
00033 public static T CurrentPage<T>(this RequestContext context) where T : ContentItem
00034 {
00035 return context.CurrentItem<T>(ContentRoute.ContentPageKey);
00036 }
00037
00038 internal static T CurrentItem<T>(this RequestContext context, string key) where T : ContentItem
00039 {
00040 if (context == null) throw new ArgumentNullException("context");
00041
00042 return context.RouteData.DataTokens[key] as T
00043 ?? context.RouteData.Values.CurrentItem<T>(key, context.RouteData.GetEngine().Persister);
00044 }
00045
00046 internal static IEngine GetEngine(this RouteData routeData)
00047 {
00048 return routeData.DataTokens[ContentRoute.ContentEngineKey] as IEngine
00049 ?? N2.Context.Current;
00050 }
00051
00052 internal static T ResolveService<T>(this RouteData routeData) where T : class
00053 {
00054 return routeData.GetEngine().Resolve<T>();
00055 }
00056
00057 internal static T[] ResolveServices<T>(this RouteData routeData) where T : class
00058 {
00059 return routeData.GetEngine().Container.ResolveAll<T>();
00060 }
00061 }
00062 }