Example #1
0
 /// <summary>
 /// Returns a form with a delete button, plus a hidden anchor we can display via Ajax to make a delete link that does a post.
 /// </summary>
 public static HtmlString FormPostLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues)
 {
     var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
     string url = urlHelper.Action(actionName, controllerName, routeValues);
     string format = @"<form method=""post"" action=""{0}"" class=""formPostLink""><input type=""submit"" value=""{1}"" />{2}</form>";
     string form = string.Format(format, helper.AttributeEncode(url), helper.AttributeEncode(linkText), helper.AntiForgeryToken());
     return new HtmlString(form + helper.ActionLink(linkText, actionName, controllerName, routeValues, new { @class = "formPostLink" }).ToString());
 }
Example #2
0
        public static MvcHtmlString Gravatar(this HtmlHelper html, string email, string name, int size = 72)
        {
            var imgUrl = string.Format(
                "http://www.gravatar.com/avatar/{0}.jpg?s={1}&d=mm&r=g",
                HashString(email),
                size);

            return new MvcHtmlString("<img alt=\"" + html.AttributeEncode(name) + "\" src=\"" + html.AttributeEncode(imgUrl) + "\" />");
        }
 public static HtmlString Script(this HtmlHelper helper, string fileName)
 {
     if (!fileName.EndsWith(".js"))
         fileName += ".js";
     var jsPath = string.Format("<script src='{0}/{1}/{2}' ></script>\n", Url.SiteUrl(pubDir), scriptDir, helper.AttributeEncode(fileName));
     return new HtmlString(jsPath);
 }
Example #4
0
 public static MvcHtmlString 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", VirtualPathUtility.ToAbsolute(pubDir), cssDir, helper.AttributeEncode(fileName));
     return MvcHtmlString.Create(jsPath);
 }
 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", pubDir, cssDir, helper.AttributeEncode(fileName));
     return jsPath;
 }
Example #6
0
 public static HtmlString 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", GetPublicUrl(helper),  cssDir, helper.AttributeEncode(fileName));
     return new HtmlString(jsPath);
 }
Example #7
0
        public static MvcHtmlString Gravatar(this HtmlHelper html, string email, string name, int size = 72)
        {
            var fallBack = WebConfigurationManager.AppSettings["GravatarFallBack"];
            if (string.IsNullOrEmpty(fallBack))
            {
                fallBack = "mm";
            }

            var imgUrl = string.Format(
                "https://secure.gravatar.com/avatar/{0}.png?s={1}&d={2}&r=g",
                HashString(email),
                size,
                fallBack);

            return new MvcHtmlString("<img alt=\"\" title=\"" + html.AttributeEncode(name) + "\" src=\"" + html.AttributeEncode(imgUrl) + "\" />");
        }
 public static string CSS(this HtmlHelper helper, string root, string fileName, string media)
 {
     if (!fileName.EndsWith(".css"))
         fileName += ".css";
     var jsPath = string.Format("<link rel='stylesheet' type='text/css' href='{0}/{1}/{2}/{3}'  media='{4}'/>\n", root, pubDir, cssDir, helper.AttributeEncode(fileName), media);
     return jsPath;
 }
 public static IHtmlString EmbedImageExtended(this HtmlHelper html, string pathOrUrl, string alt = "", string height = "200px",  string width = "200px")
 {
     if (string.IsNullOrWhiteSpace(pathOrUrl))
         throw new ArgumentException(@"Path or URL required", "pathOrUrl");
     if (IsFileName(pathOrUrl))
         pathOrUrl = html.ViewContext.HttpContext.Server.MapPath(pathOrUrl);
     return new HtmlString(string.Format("<img src=\"cid:{0}\" alt=\"{1}\"/>", (object)((ImageEmbedder)html.ViewData["Postal.ImageEmbedder"]).AddImage(pathOrUrl, (string)null).ContentId, (object)html.AttributeEncode(alt)));
 }
Example #10
0
        public static HtmlString ActionLinkWithImage(this HtmlHelper html, string imgName)
        {
            var imgUrl = string.Format("{0}/{1}/{2}"
                                       , ContentDir
                                       , ImageDir
                                       , html.AttributeEncode(imgName));

            return ActionLinkWithImage(imgUrl, "/");
        }
        public static string FullCss(this HtmlHelper helper, string basePath, string fileName)
        {
            if (!fileName.EndsWith(".css"))
                fileName += ".css";

            basePath = VirtualPathUtility.ToAbsolute("~/") + basePath;
            basePath = basePath + "public";

            var jsPath = string.Format("<link rel='stylesheet' type='text/css' href='{0}/{1}/{2}'/>", basePath, cssDirectory, helper.AttributeEncode(fileName));
            return jsPath;
        }
Example #12
0
        /// <summary>
        /// Embeds the given image into the email and returns an HTML &lt;img&gt; tag referencing the image.
        /// </summary>
        /// <param name="html">The <see cref="HtmlHelper"/>.</param>
        /// <param name="imagePathOrUrl">An image file path or URL. A file path can be relative to the web application root directory.</param>
        /// <param name="alt">The content for the &lt;img alt&gt; attribute.</param>
        /// <returns>An HTML &lt;img&gt; tag.</returns>
        public static IHtmlString EmbedImage(this HtmlHelper html, string imagePathOrUrl, string alt = "")
        {
            if (string.IsNullOrWhiteSpace(imagePathOrUrl)) throw new ArgumentException("Path or URL required", "imagePathOrUrl");

            if (IsFileName(imagePathOrUrl))
            {
                imagePathOrUrl = html.ViewContext.HttpContext.Server.MapPath(imagePathOrUrl);
            }
            var imageEmbedder = (ImageEmbedder)html.ViewData["Postal.ImageEmbedder"];
            var resource = imageEmbedder.ReferenceImage(imagePathOrUrl);
            return new HtmlString(string.Format("<img src=\"cid:{0}\" alt=\"{1}\"/>", resource.ContentId, html.AttributeEncode(alt)));
        }
Example #13
0
		public static string HighlightLink(this HtmlHelper helper, string url)
		{
			var fullUrl = url;
			if (!url.Contains("://") && !url.StartsWith("mailto:"))
			{
				if (IsEmailUrl(url))
					fullUrl = "mailto:" + url;
				else
					fullUrl = "http://" + url;
			}
			return $"<a href=\"{helper.AttributeEncode(HttpUtility.HtmlDecode(fullUrl))}\">{url}</a>";
		}
        public static string Css(this HtmlHelper helper, string fileName, bool minify)
        {
            if (!fileName.EndsWith(".css"))
                fileName += ".css";

            if (minify) {
                fileName = fileName.Insert(fileName.LastIndexOf('.'), ".min");
            }

            var jsPath = string.Format("<link rel='stylesheet' type='text/css' href='{0}/{1}/{2}' />", publicDirectory, cssDirectory, helper.AttributeEncode(fileName));
            return jsPath;
        }
Example #15
0
        public static HtmlString ActionLinkWithImage(this HtmlHelper html, string imgName, string action, string controller)
        {
            var href = string.Format("{0}/{1}"
                                     , action
                                     , controller);

            var imgUrl = string.Format("{0}/{1}/{2}"
                                       , ContentDir
                                       , ImageDir
                                       , html.AttributeEncode(imgName));

            return ActionLinkWithImage(imgUrl, href);
        }
Example #16
0
        public static MvcHtmlString Css(this HtmlHelper helper, string fileName)
        {
            var builder = new TagBuilder("link");

            var href = string.Format("{0}/{1}"
                                     , CssDir
                                     , helper.AttributeEncode(fileName));

            builder.MergeAttribute("rel", "stylesheet");
            builder.MergeAttribute("type", "text/css");
            builder.MergeAttribute("href", href);

            var output = new MvcHtmlString(builder.ToString());
            return output;
        }
Example #17
0
        public static MvcHtmlString Image(this HtmlHelper helper, string fileName, string alt, object htmlAttributes)
        {
            var builder = new TagBuilder("img");

            var src = string.Format("{0}/{1}"
                                    , ImageDir
                                    , helper.AttributeEncode(fileName));

            builder.MergeAttribute("src", src);
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

            if (alt.Length > 0)
                builder.MergeAttribute("alt", alt);

            var output = new MvcHtmlString(builder.ToString());
            return output;
        }
Example #18
0
        /// <summary>
        /// Generates a media type attribute suitable for an &lt;object&gt; or &lt;embed&gt; tag. The media type will be included in the CSP plugin-types directive.
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="mediaType">The media type.</param>
        public static IHtmlString CspMediaType(this HtmlHelper helper, string mediaType)
        {
            new Rfc2045MediaTypeValidator().Validate(mediaType);

            var context = helper.ViewContext.HttpContext;
            var cspConfigurationOverrideHelper = new CspConfigurationOverrideHelper();
            var headerOverrideHelper = new HeaderOverrideHelper();

            var configOverride = new CspPluginTypesOverride() { Enabled = true, InheritMediaTypes = true, MediaTypes = new[] { mediaType } };
            cspConfigurationOverrideHelper.SetCspPluginTypesOverride(context, configOverride, false);
            cspConfigurationOverrideHelper.SetCspPluginTypesOverride(context, configOverride, true);

            headerOverrideHelper.SetCspHeaders(context, false);
            headerOverrideHelper.SetCspHeaders(context, true);
            var attribute = string.Format("type=\"{0}\"", helper.AttributeEncode(mediaType));
            return new HtmlString(attribute);
        }
 public static MvcHtmlString SubmitButton(this HtmlHelper helper, string buttonText, object htmlAttributes = null)
 {
     StringBuilder html = new StringBuilder();
     html.AppendFormat("<input type = 'submit' value = '{0}' ", buttonText);
     //{ class = btn btn-default, id = create-button }
     var attributes = helper.AttributeEncode(htmlAttributes);
     if (!string.IsNullOrEmpty(attributes))
     {
         attributes = attributes.Trim('{', '}');
         var attrValuePairs = attributes.Split(',');
         foreach (var attrValuePair in attrValuePairs)
         {
             var equalIndex = attrValuePair.IndexOf('=');
             var attrValue = attrValuePair.Split('=');
             html.AppendFormat("{0}='{1}' ", attrValuePair.Substring(0, equalIndex).Trim(), attrValuePair.Substring(equalIndex + 1).Trim());
         }
     }
     html.Append("/>");
     return new MvcHtmlString(html.ToString());
 }
 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 FullImage(this HtmlHelper helper, string basePath, string fileName, string attributes)
        {
            if (!fileName.EndsWith(".css"))
                fileName += ".css";

            basePath = VirtualPathUtility.ToAbsolute("~/") + basePath;
            basePath = basePath + "public";

            return string.Format("<img src='{0}' {1} />", helper.AttributeEncode(fileName), helper.AttributeEncode(attributes));
        }
        public static string ModelImage(this HtmlHelper helper, string fullPathAndFileName, string attributes)
        {
            if (string.IsNullOrEmpty(fullPathAndFileName))
                return "";

            string imageUrl = helper.ViewContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute(fullPathAndFileName.Insert(0, "~"));

            return string.Format("<img src='{0}' {1} />", helper.AttributeEncode(imageUrl), helper.AttributeEncode(attributes));
        }
        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");
        }
Example #24
0
        public static HtmlString Script(this HtmlHelper helper, string fileName)
        {
            var builder = new TagBuilder("script");

            var src = string.Format("{0}/{1}/{2}"
                                    , ContentDir
                                    , ScriptDir
                                    , helper.AttributeEncode(fileName));

            builder.MergeAttribute("src", src);
            builder.MergeAttribute("type", "text/javascript");

            return new MvcHtmlString(builder.ToString());
        }
        public static string SyndicationIcons(this HtmlHelper helper, string rssUrl, string atomUrl)
        {
            StringBuilder html = new StringBuilder();

            if (!string.IsNullOrEmpty(rssUrl) || !string.IsNullOrEmpty(atomUrl))
            {
                UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

                html.Append("<div class=\"feed\">");

                Action<string, string> addIcon = (type, url) =>
                {
                    string iconUrl = urlHelper.Image("{0}.jpg".FormatWith(type));

                    html.Append(" <a href=\"{0}\" target=\"_blank\"><img alt=\"{1}\" title=\"{1}\" src=\"{2}\"/></a>".FormatWith(helper.AttributeEncode(url), type, helper.AttributeEncode(iconUrl)));
                };

                if (!string.IsNullOrEmpty(atomUrl))
                {
                    addIcon("atom", atomUrl);
                }

                if (!string.IsNullOrEmpty(rssUrl))
                {
                    addIcon("rss", rssUrl);
                }

                html.Append("</div>");
            }

            return html.ToString();
        }
 public static string Image(this HtmlHelper helper, string fileName, object htmlAttributes)
 {
     fileName = string.Format("{0}/{1}/{2}", pubDir, imageDir, fileName);
     return string.Format("<img src=\"{0}\" {1} />", helper.AttributeEncode(fileName), ConvertObjectToAttributeList(htmlAttributes));
 }
 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", pubDir, scriptDir, helper.AttributeEncode(fileName));
     return jsPath;
 }
 public static string Script(this HtmlHelper helper, string fileName, bool async)
 {
     if (!fileName.EndsWith(".js"))
         fileName += ".js";
     var jsPath = string.Format("<script src='{0}/{1}/{2}' {3}></script>", publicDirectory, scriptDirectory, helper.AttributeEncode(fileName), async ? "async" : "");
     return jsPath;
 }
Example #29
0
 public static HtmlString Image(this HtmlHelper helper, string fileName, string attributes)
 {
     fileName = string.Format("{0}/{1}/{2}", GetPublicUrl(helper),  imageDir, fileName);
     return new HtmlString(string.Format("<img src='{0}' {1} />", helper.AttributeEncode(fileName), helper.AttributeEncode(attributes)));
 }
Example #30
0
 public static MvcHtmlString Attribute(this HtmlHelper htmlHelper, string attribute)
 {
     return MvcHtmlString.Create(htmlHelper.AttributeEncode(attribute));
 }