azureasp.net-mvc-4ajaxmin

AjaxMin not working on Azure site


I am using Microsoft's AjaxMin to minify javascript on my website, which is hosted by Azure. I am using a BuildTask to automatically minify javascript at run time. This build task is specified in the .csproj file.

The process is working on my local environment, however, it does not work when I deploy to my Azure site. The azure site throws 404: file not found errors, when i try to reference the minified version of .js files.

Is it possible to use build tasks on an Azure site? Is there anything I am missing? I have made sure not to include the .min.js files in source control as this (http://www.asp.net/ajaxlibrary/AjaxMinQuickStart.ashx) tutorial suggests, but I am wondering if there is anything specific to Azure that I need to set up.

Thanks!


Solution

  • I've got this working properly in my projects. I'll tell you how I did it, though this may not be the simplest or most straightforward way.

    Before we get started, it's helpful to be able to check if your minified files are included in the Azure deployment package without actually deploying. It's pretty easy to do. The .cspkg file is actually a zip-formatted file, so you can open it with any zip archiver. (I like to use 7Zip for this because the right-click -> Open Archive command doesn't require you to rename the file, but you could use Windows Explorer, WinRAR, etc.) Inside the .cspkg you'll see another large file with a .cssx extension. That's a zip file too. Inside of the .cssx you'll find a sitesroot folder with a subdirectory for each website you're deploying, which will contain all your actual website files. So you can poke around in there and see what files are being deployed to Azure.

    First, try editing the project file for your web project (the one that contains all the Javascript/CSS files). You can use Notepad, or in Visual Studio right-click the project, select "Unload Project", then right-click again and select "Edit ". Inside the project file, insert a section like this:

    <ItemGroup>
        <!-- Copy over all the minified CSS & JS to the output directory-->
        <Content Include="**\*.min.css" />
        <Content Include="**\*.min.js" />
    </ItemGroup>
    

    Then reload the project, repackage it, and see if your files are included in the .cspkg file. If they are, then you're done.

    If not, there are a couple other things to check. Your minification might not be running at the right build stage. My minification target looks like this:

    <Target Name="PrepWebApp" Condition="$(Configuration)=='Release'" AfterTargets="AfterBuild">
    

    If that's still not working and your Web Role has multiple Sites and/or Virtual Applications in it, it's possible that the packaging steps are not running for all of the sites. So when you go to package your project for deployment to Azure, it may still not be running the minification step (along with the web.config transformations, and some other things). If that's the case, see this blog post for a way to fix it.

    Just in case that blog post goes away, I'll copy the most relevant bit here. You would put this in the .ccproj file for your web role (with appropriate bits changed to match your project structure):

    <PropertyGroup>
      <!-- Inject the publication of "secondary" sites into the Windows Azure build/project packaging process. -->
      <CoreBuildDependsOn>
        CleanSecondarySites;
        PublishSecondarySites;
        $(CoreBuildDependsOn)
      </CoreBuildDependsOn>
      <!-- This is the directory within the web application project directory to which the project will be "published" for later packaging by the Azure project. -->
      <SecondarySitePublishDir>azure.publish\</SecondarySitePublishDir>
    </PropertyGroup>
    <!-- These SecondarySite items represent the collection of sites (other than the web application associated with the role) that need special packaging. -->
    <ItemGroup>
      <SecondarySite Include="..\WebApplication1\WebApplication1.csproj" />
      <SecondarySite Include="..\WebApplication2\WebApplication2.csproj" />
    </ItemGroup>
    <Target Name="CleanSecondarySites">
      <RemoveDir Directories="%(SecondarySite.RootDir)%(Directory)$(SecondarySitePublishDir)" />
    </Target>
    <Target Name="PublishSecondarySites" Condition="'$(PackageForComputeEmulator)' == 'true'
                          Or '$(IsExecutingPublishTarget)' == 'true' ">
      <!--
        Execute the Build (and more importantly the _WPPCopyWebApplication) target to "publish" each secondary web application project.
    
        Note the setting of the WebProjectOutputDir property; this is where the project will be published to be later picked up by CSPack.
      -->
      <MSBuild Projects="%(SecondarySite.Identity)" Targets="Build;_WPPCopyWebApplication" Properties="Configuration=$(Configuration);Platform=$(Platform);WebProjectOutputDir=$(SecondarySitePublishDir)" />