0

I have a Windows Azure application. When I run locally I would like to run from a local version of jQuery. When running on a Microsoft server I would like to use the Google CDN.

Is there a good way for my razor view to switch between local and CDN versions of jQuery depending if my app is running locally or on Azure servers?

Angela
  • 3,269
  • 3
  • 22
  • 24
  • I was going to suggest a Razor helper that you'd use instead of Url.Content or Scripts.Render. That helper could reference ServiceRuntime.RoleEnvironment.IsEmulated and return either a local URL or one prefaced by your CDN endpoint. I think it *should* work, but I'm not able to resolve ServiceRuntime in the helper - thinks I need an assembly reference. Still working on it and will post as answer if I figure out what I'm doing wrong. – Jim O'Neil Sep 09 '12 at 03:32

2 Answers2

0

A content delivery network (CDN), can be used for offloading content to a globally distributed network of servers, ensuring faster throughput to your end users.

So a CDN is all about putting data as close as possible to the user, I'm not sure how Google CDN works, but if it's anything like Azure, then you need to get your content into the CDN and get a URL to the content. In your Web App you then point to the Google CDN URL to serve the context via the CDN geographically.

Take a look at this blog post on how it works for Azure 'Using the Windows Azure Content Delivery Network'.

You should be able to use the same approach but substituting in the Google CDN locations?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user728584
  • 2,135
  • 2
  • 21
  • 24
  • Thanks but this doesn't really answer the question which was "s there a good way for my razor view to switch between local and CDN versions of jQuery depending if my app is running locally or on Azure servers?" When I am developing I may be offline to the internet so I need my razor application to know not to go to the internet to get jQuery. I realize I could let it try to go to the internet and then load if it failed but I would prefer to code my application to work differently if using local emulation. – Angela Sep 09 '12 at 01:26
  • I misinterpreted your question, if you are using the Azure Emulator then you might be able to do something like this (I'm not sure how you do it in Razor mind you): http://stackoverflow.com/questions/2915340/how-to-detect-that-azure-application-is-running-in-development-fabric – user728584 Sep 09 '12 at 01:33
0

You could add a new helper:

@helper ScriptPath(string path)
{
    <script src="@fnScriptPath(path)" type="text/javascript"></script>
}

@functions 
{
    static string fnScriptPath(string path)
    {
        if (Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsEmulated)
            return "/Scripts/" + path;
        else
            return "http://ajax.aspnetcdn.com/ajax/jQuery" + path;
    }
}

then your pages would have:

@ScriptHelper.ScriptPath("jquery-1.7.1.js")

for instance.

IMPORTANT: for this to work, the Microsoft.WindowsAzure.ServiceRuntime assembly in your References needs to have Copy Local set to true. I also had to close and reopen the solution for the change to kick-in (and not just Build); otherwise, Microsoft.WindowsAzure.ServiceRuntime didn't resolve.

enter image description here

Jim O'Neil
  • 23,344
  • 7
  • 42
  • 67