Created
May 11, 2011 20:46
-
-
Save jonezy/967308 to your computer and use it in GitHub Desktop.
Helpers for MVC projects (rob conery would recognize these)
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
| using System.Web.Routing; | |
| using System.Web.Mvc.Html; | |
| namespace System.Web.Mvc { | |
| public static class HtmlHelpers { | |
| static string BaseUrl { get { return HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/"); } } | |
| static string publicDirectory = VirtualPathUtility.ToAbsolute("~/") + "public"; | |
| static string scriptDirectory = "javascript"; | |
| static string cssDirectory = "css"; | |
| static string imageDirectory = "images"; | |
| public static string Script(this HtmlHelper helper, string fileName) { | |
| if (!fileName.EndsWith(".js")) | |
| fileName += ".js"; | |
| var jsPath = string.Format("<script src='{0}/{1}/{2}' ></script>\n", publicDirectory, scriptDirectory, helper.AttributeEncode(fileName)); | |
| return jsPath; | |
| } | |
| public static string Css(this HtmlHelper helper, string fileName) { | |
| return Css(helper, fileName, "screen"); | |
| } | |
| public static string Css(this HtmlHelper helper, string fileName, string media) { | |
| if (!fileName.EndsWith(".css")) | |
| fileName += ".css"; | |
| var jsPath = string.Format("<link rel='stylesheet' type='text/css' href='{0}/{1}/{2}' media='" + media + "'/>\n", publicDirectory, cssDirectory, helper.AttributeEncode(fileName)); | |
| return jsPath; | |
| } | |
| public static string Image(this HtmlHelper helper, string fileName) { | |
| return Image(helper, fileName, ""); | |
| } | |
| public static string Image(this HtmlHelper helper, string fileName, string attributes) { | |
| fileName = string.Format("{0}/{1}/{2}", publicDirectory , imageDirectory, fileName); | |
| return string.Format("<img src='{0}' {1} />", helper.AttributeEncode(fileName), helper.AttributeEncode(attributes)); | |
| } | |
| public static string ImageLink(this HtmlHelper helper, string fileName, string imageAttributes) { | |
| return ImageLink(helper, fileName, imageAttributes, false); | |
| } | |
| public static string ImageLink(this HtmlHelper helper, string fileName, string imageAttributes, bool openInNewWindow) { | |
| fileName = string.Format("{0}/{1}/{2}", publicDirectory, imageDirectory, fileName); | |
| return string.Format("<a href='{0}' target='{2}'><img src='{0}' {1} /></a>", helper.AttributeEncode(fileName), helper.AttributeEncode(imageAttributes), openInNewWindow ? "_blank" : "_self"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment