Specializations

Thursday, March 28, 2013

ASP.NET MVC Extensions for CSS



Introduction


Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Objective


How to create and use extension for css

Using the code

Step1: Create a project for extension
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Web.Mvc;
 
namespace MyExtensions
{
    public static class CssExtensions
    {
        private static readonly string defaultContentURL = @"http://localhost:50900/Content";
 
        public static MvcHtmlString Css(this HtmlHelper helper, string cssfile)
        {
            return Css(helper, cssfile, null);
        }
 
        public static MvcHtmlString Css(this HtmlHelper helper, string cssfile, string mediaType)
        {
            string cssUrl = defaultContentURL + @"/Css";
            if (String.IsNullOrEmpty(cssfile))
            {
                throw new ArgumentException();
            }
            string src;
            if (IsRelativeToDefaultPath(cssfile))
            {
                src = "~/Content/Css" + cssfile;
            }
            else
            {
                src = string.Format("{0}{1}", (ConfigurationManager.AppSettings["ContentURL"] + @"/Css").Or(cssUrl), cssfile);                
            }
            TagBuilder linkTag = new TagBuilder("link");
            linkTag.MergeAttribute("type""text/css");
            linkTag.MergeAttribute("rel""stylesheet");
            if (mediaType != null)
            {
                linkTag.MergeAttribute("media", mediaType);
            }
            linkTag.MergeAttribute("href"UrlHelper.GenerateContentUrl(src, helper.ViewContext.HttpContext));
            return MvcHtmlString.Create(linkTag.ToString(TagRenderMode.SelfClosing));
        }
 
        internal static bool IsRelativeToDefaultPath(string file)
        {
            return !(file.StartsWith("~"StringComparison.Ordinal) ||
                file.StartsWith("../"StringComparison.Ordinal) ||
                file.StartsWith("/"StringComparison.Ordinal) ||
                file.StartsWith("http://"StringComparison.OrdinalIgnoreCase) ||
                file.StartsWith("https://"StringComparison.OrdinalIgnoreCase));
        }
 
        public static string Or(this string text, string alternateText,
           bool allowEmptyText = falsebool paramCheckAlternateText = true)
        {
            if (paramCheckAlternateText && string.IsNullOrEmpty(alternateText))
                throw new ArgumentNullException("alternateText");
 
            var useAlternate = allowEmptyText ? (text == null) : string.IsNullOrEmpty(text);
            return useAlternate ? alternateText : text;
        }
    }
}
Refer the dll of the above code compiled in your web app
@using MyExtensions
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    @Html.Css("/Site.css")
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>

Output



Happy Implementation

Reference

http://msdn.microsoft.com/en-us/library/bb383977.aspx

No comments:

Post a Comment