Skip to content

Instantly share code, notes, and snippets.

@joelpurra
Created March 30, 2012 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelpurra/2253509 to your computer and use it in GitHub Desktop.
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
//-----------------------------------------------------------------------
// <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>
/// &lt;body id="@this.Html.GetPageId()"&gt;
/// </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>
/// &lt;body class="page-type @this.Html.GetPageTypeClasses() any-other-class-you-like"&gt;
/// </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