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

      Idea R - Do others know who you are? Design your corporate identity
  • HomeHome
  • About UsAbout Us
  • ContactsContacts

Blog

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

Change language... italiano

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 to dramatically increase social media LIKEs to your web site

Here's a free download that I created some time ago. It's a good tool for increasing engagement, but use with caution.

Last published

Can engineering industry sponsor art without going off topic?

Can engineering industry sponsor art without going off topic?

How to design a page in an exhibition catalog of Biennale in Venice, for a customer who apparently has nothing to do with art.

Milano Design Week 2017, Design Districts

Ventura Lambrate and Tortona, here are the best creations that we discovered in the 2 districts of the Fuorisalone Milan Design Week 2017.

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

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.
newsletter

Stay informed on digital strategies

My reviews on the subject...

I have many books on web design, the list is quite long, especially if I have to include all those which deal strictly to programming. I shall therefore refer you to some interesting texts. Don't Make Me Think! is the most famous book on usability, you can not miss it if you're involved in web design.

RSS feed

How to skip the CDN during ASP.NET development

Published on 12/19/2015
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>
    [ExpressionPrefix("CdnUrl")]
    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",
                UriKind.Absolute);
            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;
        }
        #endregion
        #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)
                [entry.PropertyInfo.Name];
            return new CodeCastExpression(descriptor.PropertyType,
                new CodeMethodInvokeExpression(
                    new CodeTypeReferenceExpression(GetType()),
                    "GetCdnUrl", expressionArray));
        }     
        #endregion
    }
}

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

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

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 1,578.

Comments

comments powered by Disqus
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
Blog

Scroll to top