Created
March 30, 2012 18:05
-
-
Save joelpurra/2253509 to your computer and use it in GitHub Desktop.
UrlCssHelper: A helper extending ASP.NET MVC3 with simple ID and CSS class generators for page level elements, to make per-page or per-feature CSS includes/rules simpler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------------------------------------------- | |
// <copyright file="UriCssHelper.cs" company="The Swedish Post and Telecom Authority (PTS)"> | |
// Copyright (c) 2011, 2012 The Swedish Post and Telecom Authority (PTS) | |
// Developed for PTS by Joel Purra <http://joelpurra.se/> | |
// Released under the BSD license. | |
// </copyright> | |
//----------------------------------------------------------------------- | |
// https://gist.github.com/2253509 | |
namespace JoelPurra.Web.Helpers | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics.Contracts; | |
using System.Web; | |
using System.Web.Mvc; | |
public static class UrlCssHelper | |
{ | |
#region Constants and Fields | |
private const char UrlSegmentDelimiter = '/'; | |
private const string IdPrefix = "url-"; | |
#endregion | |
#region Public Methods | |
/// <example> | |
/// <body id="@this.Html.GetPageId()"> | |
/// </example> | |
public static MvcHtmlString GetPageId(this HtmlHelper htmlHelper) | |
{ | |
Contract.Requires(htmlHelper != null); | |
IEnumerable<string> parts = htmlHelper.GetUsableUrlParts(); | |
string id = IdPrefix + string.Join("-", parts); | |
return new MvcHtmlString(id); | |
} | |
/// <example> | |
/// <body class="page-type @this.Html.GetPageTypeClasses() any-other-class-you-like"> | |
/// </example> | |
public static MvcHtmlString GetPageTypeClasses(this HtmlHelper htmlHelper) | |
{ | |
Contract.Requires(htmlHelper != null); | |
IEnumerable<string> parts = htmlHelper.GetUsableUrlParts(); | |
string classes = string.Join(" ", parts); | |
return new MvcHtmlString(classes); | |
} | |
public static Uri GetUrl(this HtmlHelper htmlHelper) | |
{ | |
Contract.Requires(htmlHelper != null); | |
Contract.Requires(htmlHelper.ViewContext != null); | |
Contract.Requires(htmlHelper.ViewContext.HttpContext != null); | |
Contract.Requires(htmlHelper.ViewContext.HttpContext.Request != null); | |
Contract.Requires(htmlHelper.ViewContext.HttpContext.Request.Url != null); | |
Uri url = htmlHelper.ViewContext.HttpContext.Request.Url; | |
return url; | |
} | |
public static Uri UrlOriginal(this HttpRequestBase request) | |
{ | |
Contract.Requires(request != null); | |
Contract.Requires(request.Url != null); | |
string hostHeader = request.Headers["host"]; | |
return new Uri(string.Format("{0}://{1}{2}", request.Url.Scheme, hostHeader, request.RawUrl)); | |
} | |
#endregion | |
#region Methods | |
private static IEnumerable<string> GetUsableUrlParts(this HtmlHelper htmlHelper) | |
{ | |
Uri url = htmlHelper.GetUrl(); | |
return url.GetUsableUrlParts(); | |
} | |
private static IEnumerable<string> GetUsableUrlParts(this Uri url) | |
{ | |
IList<string> parts = new List<string>(); | |
foreach (string segment in url.Segments) | |
{ | |
string clean = segment.Trim(UrlSegmentDelimiter).ToLowerInvariant(); | |
int routeId; | |
if (int.TryParse(clean, out routeId)) | |
{ | |
continue; | |
} | |
if (!string.IsNullOrWhiteSpace(clean)) | |
{ | |
parts.Add(clean); | |
} | |
} | |
return parts; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment