Skip to content

Instantly share code, notes, and snippets.

@timiles
timiles / gist:2828976
Created May 29, 2012 15:15
RenderRazorViewToString extension
// credit: http://stackoverflow.com/a/2759898/487544
public static string RenderRazorViewToString(this Controller controller, string viewName, object model)
{
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
@timiles
timiles / gist:2829059
Created May 29, 2012 15:27
nice css snippets
/*-- js/nojs with Modernizr --*/
html.js .js-hide, .no-js-hide
{
display: none;
}
html.js .no-js-hide
{
display: inherit;
}
@timiles
timiles / gist:2829104
Created May 29, 2012 15:32
nice js snippets
/*-- to detect mobile size in js --*/
/* credit: http://bricss.net/post/22198838298/easily-checking-in-javascript-if-a-css-media-query-has */
function isMobileSize() {
var size = window.getComputedStyle(document.body, ':after').getPropertyValue('content');
return size.indexOf('mobile') != -1;
}
/*
@media (max-width: 767px)
{
body:after
@timiles
timiles / gist:2835527
Created May 30, 2012 11:02
eg ModelState.Try("model.Email", () => user.ChangeEmail(newEmail));
// credit: @mcintyre321
public static class ModelStateDictionaryExtensions
{
public static bool Try(this ModelStateDictionary ms, string key, Action action)
{
try
{
action();
return true;
}
@timiles
timiles / gist:2851684
Created June 1, 2012 12:13
MenuActionLink HtmlHelper Extensions
/// <summary>
/// adds the active class if the link's action & controller matches current request
/// </summary>
public static MvcHtmlString MenuActionLink(this HtmlHelper htmlHelper,
string linkText, string actionName, string controllerName,
object routeValues = null, object htmlAttributes = null,
string activeClassName = "active")
{
IDictionary<string, object> htmlAttributesDictionary =
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
@timiles
timiles / gist:2851789
Created June 1, 2012 12:28
Url.AbsoluteAction helper
public static string AbsoluteAction(this UrlHelper url,
string actionName, string controllerName, object routeValues = null, bool isHttps = false)
{
return url.Action(actionName, controllerName, routeValues, "http" + (isHttps ? "s" : ""));
}
@timiles
timiles / gist:2936923
Last active October 6, 2015 04:28
String helper: Truncate with ellipsis
public static string Truncate(this string s, int maxLength, bool withEllipsis = false)
{
if (maxLength < 0) throw new ArgumentOutOfRangeException(nameof(maxLength), "must be at least zero");
if (s == null || s.Length <= maxLength) return s;
if (withEllipsis && maxLength > 3)
{
return s.Substring(0, maxLength - 3) + "...";
}
return s.Substring(0, maxLength);
}
@timiles
timiles / gist:2941647
Created June 16, 2012 15:20
String helper: to url fragment
// credit: http://www.extensionmethod.net/Details.aspx?ID=453
public static string ToUrlFragment(this string s)
{
if (String.IsNullOrEmpty(s)) return "";
// Unicode Character Handling: http://blogs.msdn.com/b/michkap/archive/2007/05/14/2629747.aspx
string stFormD = s.Trim().ToLowerInvariant().Normalize(NormalizationForm.FormD);
var sb = new StringBuilder();
foreach (var t in from t in stFormD let uc = CharUnicodeInfo.GetUnicodeCategory(t) where uc != UnicodeCategory.NonSpacingMark select t)
{
sb.Append(t);
@timiles
timiles / gist:3065794
Created July 7, 2012 10:33
ToUnderscoreCase / ToHyphenCase
/// <summary>
/// Converts PascalCase and camelCase to underscore_case. Leaves underscore_case unaffected.
/// </summary>
/// <param name="s">the string to convert</param>
/// <returns>the underscore_cased string</returns>
internal static string ToUnderscoreCase(this string s)
{
return Regex.Replace(s, @"(\p{Ll})(\p{Lu})", "$1_$2").ToLower();
}
@timiles
timiles / InMemoryTableStorage.cs
Last active December 9, 2015 12:37
use Microsoft.Experience.CloudFx.Framework.Storage.InMemoryCloudTableStorage
// use Microsoft.Experience.CloudFx.Framework.Storage.InMemoryCloudTableStorage