Extensions for CSS

Shantanupatel
Posted by in ASP.NET MVC category on for Intermediate level | Points: 250 | Views : 7073 red flag
Rating: 5 out of 5  
 4 vote(s)

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

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


 Download source code for 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

Page copy protected against web site content infringement by Copyscape

About the Author

Shantanupatel
Full Name: Shantanu Patel
Member Level:
Member Status: Member
Member Since: 2/19/2010 8:01:15 AM
Country: India
Shantanu
http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Thanos on: 6/10/2019 | Points: 25
You can just need to follow here http://solitairetimes.com and seen the all function for play free soiltaire card game it is the great fun for the all players.

Login to post response

Comment using Facebook(Author doesn't get notification)