Language: C#
ASP.NET MVC 2 Extension Methods - Where am I?
1: using System; 2: using System.Linq; 3: using System.Text; 4: using System.Web.Mvc; 5: using System.Web.Mvc.Html; 6: using System.Web.Routing; 7: 8: namespace MvcSamples.Html 9: { 10: public static class UrlExtensions 11: { 12: public static bool IsCurrent(this UrlHelper urlHelper, String areaName) 13: { 14: return urlHelper.IsCurrent(areaName, null, null); 15: } 16: 17: public static bool IsCurrent(this UrlHelper urlHelper, String areaName, String controllerName) 18: { 19: return urlHelper.IsCurrent(areaName, controllerName, null); 20: } 21: 22: public static bool IsCurrent(this UrlHelper urlHelper, String areaName, String controllerName, params String[] actionNames) 23: { 24: return urlHelper.RequestContext.IsCurrentRoute(areaName, controllerName, actionNames); 25: } 26: 27: public static string Selected(this UrlHelper urlHelper, String areaName) 28: { 29: return urlHelper.Selected(areaName, null, null); 30: } 31: 32: public static string Selected(this UrlHelper urlHelper, String areaName, String controllerName) 33: { 34: return urlHelper.Selected(areaName, controllerName, null); 35: } 36: 37: public static string Selected(this UrlHelper urlHelper, String areaName, String controllerName, params String[] actionNames) 38: { 39: return urlHelper.IsCurrent(areaName, controllerName, actionNames) ? "selected" : String.Empty; 40: } 41: } 42: 43: public static class HtmlExtensions 44: { 45: public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper, String linkText, String actionName, String controllerName) 46: { 47: var tag = new TagBuilder("li"); 48: 49: if ( htmlHelper.ViewContext.RequestContext.IsCurrentRoute(null, controllerName, actionName) ) 50: { 51: tag.AddCssClass("selected"); 52: } 53: 54: tag.InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToString(); 55: 56: return MvcHtmlString.Create(tag.ToString()); 57: } 58: } 59: 60: public static class RequestExtensions 61: { 62: public static bool IsCurrentRoute(this RequestContext context, String areaName) 63: { 64: return context.IsCurrentRoute(areaName, null, null); 65: } 66: 67: public static bool IsCurrentRoute(this RequestContext context, String areaName, String controllerName) 68: { 69: return context.IsCurrentRoute(areaName, controllerName, null); 70: } 71: 72: public static bool IsCurrentRoute(this RequestContext context, String areaName, String controllerName, params String[] actionNames) 73: { 74: var routeData = context.RouteData; 75: var routeArea = routeData.DataTokens["area"] as String; 76: var current = false; 77: 78: if ( ((String.IsNullOrEmpty(routeArea) && String.IsNullOrEmpty(areaName)) || (routeArea == areaName)) && 79: ((String.IsNullOrEmpty(controllerName)) || (routeData.GetRequiredString("controller") == controllerName)) && 80: ((actionNames == null) || actionNames.Contains(routeData.GetRequiredString("action"))) ) 81: { 82: current = true; 83: } 84: 85: return current; 86: } 87: } 88: }
Tags:
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search


Please read the following blog post for more information: http://blog.bmdiaz.com/archive/2010/04/09/handy-asp.net-mvc-2-extension-methods-ndash-where-am-i.aspx