ViewData Helpers
On thing that I love and hate at the same time is the PresentationModel pattern – where you spin up a class to hold other classes which may, or may not, have some logic to it. This is one of those code explosion points and I’m beginning to shy away from the whole thing, preferring instead to “just use ViewData”. Clearly if you have a lot of data you don’t want to dump all your stuff in ViewData (like a trashcan), but if you have 2-3 lists or classes – what the hell?
public static class ViewDataExtensions{ public static IEnumerable<T> ViewData<T>(this HtmlHelper helper, string name) { if (helper.ViewData[name] != null) { return (IEnumerable<T>) helper.ViewData[name]; } return new List<T>(); } public static T ViewDataSingle<T>(this HtmlHelper helper, string name) { if (helper.ViewData[name] != null) { return (T)helper.ViewData[name]; } return default(T); }}
List Extensions
One of the things I struggle with on every single project is Html.DropDownList. I don’t know why – I just have a mental block. I think it’s because it seems like there’s a lot of extra work that goes into getting an IEnumerable into the proper position for inclusion in a DropDownList. What would be groovy is to just take an enumerable source, add an extension method that sets it up as a SortedList then call it a day!
In addition I’ve always wished that IEnumerable had the ForEach() method extension. Not sure why that only works for IList<T>. I should add here that I pulled some of the code below off the web, but I can’t for the life of me find the link for attribution (sorry):
public static class ListExtensions { public static IEnumerable<T> ForEach<T>(this IEnumerable<T> collection, Action<T> action) { foreach (var item in collection) action(item); return collection; } public static SelectList ToSelectList<T>(this IEnumerable<T> collection) { return new SelectList(collection, "Key", "Value"); } public static SelectList ToSelectList<T>(this IEnumerable<T> collection, string selectedValue) { return new SelectList(collection, "Key", "Value", selectedValue); } public static SelectList ToSelectList<T>(this IEnumerable<T> collection, string dataValueField, string dataTextField) { return new SelectList(collection, dataValueField, dataTextField); } public static SelectList ToSelectList<T>(this IEnumerable<T> collection, string dataValueField, string dataTextField, string selectedValue) { return new SelectList(collection, dataValueField, dataTextField, selectedValue); } }
Gravatar
I think everyone knows what Gravatar is all about…
public static class GravatarHelper { public static string Gravatar(this HtmlHelper helper, string email, int size) { var result = "<img src=\"{0}\" alt=\"Gravatar\" class=\"gravatar\" />"; var url = GetGravatarURL(email, size); return string.Format(result, url); } static string GetGravatarURL(string email, int size) { return (string.Format("http://www.gravatar.com/avatar/{0}?s={1}&r=PG", EncryptMD5(email), size.ToString())); } static string GetGravatarURL(string email, int size, string defaultImagePath) { return GetGravatarURL(email, size) + string.Format("&default={0}", defaultImagePath); } static string EncryptMD5(string Value) { var md5 = new MD5CryptoServiceProvider(); var valueArray = System.Text.Encoding.ASCII.GetBytes(Value); valueArray = md5.ComputeHash(valueArray); var encrypted = ""; for (var i = 0; i < valueArray.Length; i++) encrypted += valueArray[i].ToString("x2").ToLower(); return encrypted; } }
Url Helpers
To round out this long hacky list is my old, long-standing favorite: my Url Helpers. I’m sure there are probably better ways to work this, but these guys have helped me so often that I feel like I just can’t give them up.
Top of the list here is Url.SiteRoot() – this returns an absolute URL to your site – something that hasn’t been terribly easy to do for some reason. The code is based on Rick Strahl’s original bits – modified here and there over time.
The other method (GetRedirectUrl) is very, very useful when you need to augment the FormsAuth stuff on your site. We use this exact method on Tekpub when integrating Open ID.
public static class UrlHelpers { public static string SiteRoot(HttpContextBase context) { return SiteRoot(context, true); } public static string SiteRoot(HttpContextBase context, bool usePort) { var Port = context.Request.ServerVariables["SERVER_PORT"]; if (usePort) { if (Port == null || Port == "80" || Port == "443") Port = ""; else Port = ":" + Port; } var Protocol = context.Request.ServerVariables["SERVER_PORT_SECURE"]; if (Protocol == null || Protocol == "0") Protocol = "http://"; else Protocol = "https://"; var appPath = context.Request.ApplicationPath; if (appPath == "/") appPath = ""; var sOut = Protocol + context.Request.ServerVariables["SERVER_NAME"] + Port + appPath; return sOut; } public static string SiteRoot(this UrlHelper url) { return SiteRoot(url.RequestContext.HttpContext); } public static string SiteRoot(this ViewPage pg) { return SiteRoot(pg.ViewContext.HttpContext); } public static string SiteRoot(this ViewUserControl pg) { var vpage = pg.Page as ViewPage; return SiteRoot(vpage.ViewContext.HttpContext); } public static string SiteRoot(this ViewMasterPage pg) { return SiteRoot(pg.ViewContext.HttpContext); } public static string GetReturnUrl(HttpContextBase context) { var returnUrl = ""; if (context.Request.QueryString["ReturnUrl"] != null) { returnUrl = context.Request.QueryString["ReturnUrl"]; } return returnUrl; } public static string GetReturnUrl(this UrlHelper helper) { return GetReturnUrl(helper.RequestContext.HttpContext); } public static string GetReturnUrl(this ViewPage pg) { return GetReturnUrl(pg.ViewContext.HttpContext); } public static string GetReturnUrl(this ViewMasterPage pg) { return GetReturnUrl(pg.Page as ViewPage); } public static string GetReturnUrl(this ViewUserControl pg) { return GetReturnUrl(pg.Page as ViewPage); } }
Hope you find some of these useful! If so – share yours!
