views:

174

answers:

2

Hello, I am currently developing an ASP.NET web application and do most of my development on the road, i.e. offline. I plan to use Google/Microsoft/an-other CDN for JQuery and a couple of other script resources.

My question is, is there a straightforward way to develop with a link to a local file within the solution, but to point to the CDN upon deployment/release build? Thank you in advance!

A: 

You could just change the link before you deploy...?

Update:

A simple Replace All will suffice if you have a link everywhere.

I know these might be really dumb and simple solutions, but it seems to me that your problem is too simple to require an abstraction or extra code writing.

However, if you must, this is one way of doing it:

Create an XML file that holds values:

MyAppSettings.xml

<?xml version="1.0" encoding="utf-8" ?>

<MyAppSettings>
   <JqueryLink 
      value="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"
      store1="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"
      store2="../jquery.min.js"
      >
  </JqueryLink> 
</MyAppSettings>

And get the value from the XML file:

  public static string GetJqueryUrl()
  {
        XElement file = XElement.Load(HttpContext.Current.Server.MapPath("~/App_Data/MyAppSettings.xml"));
        string jquerylink = file.Element("JqueryLink").Attribute("value");
        return jquerylink;
  }

You could make a helper function for the previous code and use it all over your code.

Whenever you want to switch between deploy and offline links, just change the "value" parameter in the xml file.

You can keep the attributes "store1" and "store2" in there just so I wouldn't have to remember what they are when I do switch them.

Baddie
I am aware of that. I would like to automate the approach so that it is not necessary to identify everywhere such a link is included every time the application is deployed in the next decade. The more manual tasks I can avoid the better.
JP
Updated my answer.
Baddie
+2  A: 

You could write a helper function:

public static string JQuerySource()
{
    var config = WebConfigurationManager.OpenWebConfiguration("~");
    var compilation = config.GetSection("system.web/compilation") as CompilationSection;
    if (compilation == null || compilation.Debug)
    {
        // Running in Debug mode
        return "/scripts/jquery.js";
    }
    // Running in Release mode
    return "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
}

That you would use like this:

<script type="text/javascript" src="<%=JQuerySource() %>"></script>
Darin Dimitrov