Welcome to Idea R | Branding - Web Agency - Digital Strategies
Switch to the mobile layout

      Idea R - Do you have a real web strategy? Persuasion Technologies
  • HomeHome
  • About UsAbout Us
  • ContactsContacts


Take a software geek from the 80s, add a new generation graphic designer and dilute with a longtime marketing strategist. Shake vigorously and you'll get the Idea R's blog.

Change language... italiano
Go to the blog summaryGo to the blog summary

You may be interested in

How to fix your ASP.NET site to be responsive on Windows Phone

How to fix your ASP.NET site to be responsive on Windows Phone

Internet Explorer 10 for Windows Phone 8 suffers a bug when displaying responsive web sites. See how to fix it server-side with few lines of code.

How ASP.NET can efficiently control the client cache

How to precisely control the cache of the client in order to avoid the transmission of data from the ASP.NET server when the client already owns them.

Last published

Metalworking industry: how to multiply customers using only the website and zero advertising budget

Metalworking industry: how to multiply customers using only the website and zero advertising budget

Find out how to design a website for the metalworking industry, that will be a magnet for customers and able to triple your leads.

A simple but effective SEO strategy: the middleman

Get excellent results in a short time using this SEO trick. Lern how to quickly apply the middleman technique and have many new web pages indexed.

Most popular

Lazy loading and the SEO problem, solved!

Lazy loading and the SEO problem, solved!

The best SEO solution to index your dynamically loaded contents, no more compromises!

Case study: TAV Engineering, branding for B2B services

How to create a brand that keeps distance from the cold and aseptic image of the metal industry, focusing instead on the human aspect.

Editor's pick

Case study: branding and marketing for a magician

Case study: branding and marketing for a magician

Singular case study in the world of illusionism. See how we have created a brand and a web marketing campaign with exceptional results.
Mental marketing secret #1: the fake compromise

Mental marketing secret #1: the fake compromise

Can mentalism influence marketing? As a marketing professional and a long term magician, I will reveal you some tricks.

Stay informed on digital strategies

Take a look at the last published articles
RSS feed

How to skip the CDN during ASP.NET development

Published on 12/19/2015
Categories: Web Design
How to skip the CDN during ASP.NET development

We have already explored the benefits of using a CDN (Content Delivery Networks) to speed up your website, so I'll not address the matter again. If you want to know more about this, take a look at Irritated by your blog performance? Boost it using the cloud!

The problem that you are going to face if you decide to use a CDN, is that during development usually you don't want to use the CDN, rather you prefer to serve resource directly from your web site.

For example in the head section of your page you have the following markup.

<link href="/css/style.css" rel="stylesheet" type="text/css"/>

If you want to speed up page rendering you can download the same style sheet from the CDN

<link href="http://cdn.mycdn.com/css/style.css" rel="stylesheet" type="text/css"/>

But while you are developing the web application on your machine or while there's a  test version on your deployment slot, you want that the style sheet is downloaded directly from the website and not from the CDN.
Another problem may arise if you change your mind and you want to switch your CDN to another one.

How to rapidly switch from a CDN to another one without having to replace all URLs in all your pages?
How to solve this problem without hardcoding the CDN URL in your ASP.NET pages?

I'm going to share with you a smart and definitive solution that will save you tons of hours.

Use an Expression Builder!

I have developed an expression builder that allows me to use the following markup:

<link href="<%$ CdnUrl:/css/style.css %>" rel="stylesheet" type="text/css" />

This markup will prepend the CDN to the URL only in production and will render a local URL in all other cases.

You can use this syntax everywhere you need to serve a resource from the CDN, for example an image:

<asp:Image ImageUrl="<%$ CdnUrl:/images/myimage.png %>" runat="server" />

This magic is possible thanks to a class derived from ExpressionBuilder.

namespace IdeaR.Web.Compilation
    /// <summary>
    /// In release version prepends the CDN URL to the specified one
    /// if the domain is the production one.
    /// </summary>
    public class CdnUrlExpressionBuilder : ExpressionBuilder
        #region Operations
        /// <summary>
        /// Returns the evaluated expression.
        /// </summary>
        public static object GetCdnUrl(string expression, Type target,
            string entry)
            var retvalue = expression;
            var productionUri = new Uri("http://www.myproductionurl.com",
            var currentUri = HttpContext.Current.Request.Url;
            var cdnUrl = "http://cdn.mycdn.com";
            // If this is a production website URL
            if (currentUri.Scheme == productionUri.Scheme &&
                currentUri.Host == productionUri.Host)
                retvalue = cdnUrl + expression;
            return retvalue;
        #region Overrides
        /// <summary>
        /// Returns a code expression that is used to perform the
        /// property assignment in the generated page class.
        /// </summary>
        public override CodeExpression GetCodeExpression(
            BoundPropertyEntry entry,
            object parsedData, ExpressionBuilderContext context)
            var componentType = entry.DeclaringType;
            var expressionArray = new CodeExpression[3]
                new CodePrimitiveExpression(entry.Expression.Trim()),
                new CodeTypeOfExpression(componentType),
                new CodePrimitiveExpression(entry.Name)
            var descriptor = TypeDescriptor.GetProperties(componentType)
            return new CodeCastExpression(descriptor.PropertyType,
                new CodeMethodInvokeExpression(
                    new CodeTypeReferenceExpression(GetType()),
                    "GetCdnUrl", expressionArray));

To register the new expression you have only to place the following declaration in the web.config file

    <add expressionPrefix="CdnUrl"
          type="IdeaR.Web.Compilation.CdnUrlExpressionBuilder" />

That's all!
You can further extend it, for example if the URL is a style sheet you can append a version number to clear the CDN cache, etc.

I hope you will enjoy this piece of code, if so leave a comment or share it!

Did you find this article useful?

If you think your friends will like it too, share it on Facebook.
You have only to press the button here below!


See also

Irritated by your blog performance? Boost it using the cloud!

Are you using the world's fastest hosting? If you have a bad architecture, your blog won't have a great performance. Here is the ultimate solution.

You are the reader number 4,131.


Creative Commons License

Photographies, videos, graphics and texts by Idea R are licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Previous article

Previous article

Influencer Marketing: all you need to know

Next article

Milano Design Week 2015, Design Districts

Next article
  • Visual Communication, Art DirectionVisual Communication, Art Direction
  • Apps, Cloud, Mobile, Software DesignApps, Cloud, Mobile, Software Design
  • Digital StrategiesDigital Strategies
  • BlogBlog
Touching finger
RSS feed

Scroll to top