Last active
March 11, 2016 07:57
-
-
Save rasmuseeg/130acc3ec2c7b45a2a90 to your computer and use it in GitHub Desktop.
HtmlHelper extensions
This file contains 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
using System.Linq; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
namespace System.Web.Mvc | |
{ | |
public static class HtmlHelperExtensions | |
{ | |
#region HtmlHelper Extensions | |
public static string Email(this HtmlHelper helper, string email, string message = "JavaScript is disabled, please enable to read this email address") | |
{ | |
var regex = new Regex(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); | |
if (!regex.Match(email).Success) return null; | |
string[] parts = email.Split('@'); | |
string who = parts[0]; | |
string at = parts[1]; | |
var html = new StringBuilder(); | |
html.AppendFormat("<script>var who = '{0}', at = '{1}', e = who + '@' + at;document.write('<a href=\"mailto:'+e+'\">'+e+'</a>');</script>", who, at); | |
html.AppendFormat("<noscript>{0}</noscript>", message); | |
return html.ToString(); | |
} | |
public static IHtmlString StripTags(this HtmlHelper helper, string value, params string[] tags) | |
{ | |
string stripped = value; | |
foreach (var tag in tags) | |
{ | |
stripped = Regex.Replace(value, "<[^>]p*>", "", RegexOptions.Multiline | RegexOptions.IgnoreCase); | |
} | |
return new HtmlString(stripped); | |
} | |
public static string Tel(this HtmlHelper helper, string prefix, string number) | |
{ | |
return prefix + " " + string.Join(" ", number.SplitParts(2)); | |
} | |
/// <summary> | |
/// Modified QueryString with key to value | |
/// </summary> | |
/// <param name="helper"></param> | |
/// <param name="name"></param> | |
/// <param name="value"></param> | |
/// <returns>?key=value</returns> | |
public static IHtmlString ModifyQueryString<T>(this HtmlHelper helper, string key, T value) | |
{ | |
var request = HttpContext.Current.Request; | |
var nameValues = HttpUtility.ParseQueryString(request.QueryString.ToString()); | |
nameValues.Set(key, value.ToString()); | |
return new HtmlString("?" + nameValues.ToString()); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="helper"></param> | |
/// <param name="key">The key of the QueryString</param> | |
/// <param name="fallback">If empty or null</param> | |
/// <returns></returns> | |
public static T ParseQueryString<T>(this HtmlHelper helper, string key, T fallback) | |
{ | |
var request = HttpContext.Current.Request; | |
var nameValues = HttpUtility.ParseQueryString(request.QueryString.ToString()); | |
if (nameValues.AllKeys.Contains(key)) | |
{ | |
try | |
{ | |
return (T)Convert.ChangeType(nameValues.Get(key), typeof(T)); | |
} catch (Exception) { } | |
} | |
return fallback; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment