English 中文(简体)
Convert local (JQuery) link to CDN link at deploy time
原标题:

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!

最佳回答

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>
问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签