-aspnet not working...

Jan 18, 2012 at 8:40 PM
Edited Jan 18, 2012 at 9:55 PM
I'm trying to minify a file with asp.net tags <% %>, but it does not work. 
It compiles successfully,
but when I load a page, firebug shows errors.
For example the path for params.styleUrl = 
'<%= Master.BaseUrl%>/chat/css/black.css'; which results in a 404 error.
This is the command I am running

ajaxmin -JS -clobber chat\config.js -out scripts\optimized\chat.js

       // chat settings
       var regParams = {
           wmode: 'transparent'

          params.autojoin = '<%=liveChannelVM.Channel.ChannelName%>';
       params.nick = '<%= Master.Username%>';
       params.showNickSelection = false;
       params.styleURL = '<%= Master.BaseUrl%>/chat/css/black.css';
       params.emoticonPath = '<%= Master.BaseUrl%>/chat/emoticons/';
       params.showNavigation = false;
       params.showMenuButton = false;
       params.showInfoMessages = false;
       params.navigationPosition = 'top';
       params.userListWidth = '0';
       params.showChannelCentral = false;
       params.showJoinPartMessages = false;
       params.showTimestamps = false;

       swfobject.embedSWF("chat/lightIRC.swf", "chat", "350", "600", "10.0.0", "expressInstall.swf", params, regParams);

       // load player
       var channelId = '<%=liveChannelVM.Channel.ChannelId%>';

Jan 18, 2012 at 9:10 PM

So what kind of setup are you using? Does your JS file actually have the opening/closing <script> HTML element in it? I would think AjaxMin would choke on that if it did. Or is that just to show that the script is being embedded in an .ASPX file? And can I assume the "params" object is being created elsewhere?

Jan 18, 2012 at 9:53 PM

There is no opening and closing script tag in it.  Yes, the params object is created elsewhere. Like I said, the ajaxmin compiled and created the file successfully, but it didnt handle those tags

Jan 18, 2012 at 10:05 PM

Okay, cool. AjaxMin isn't going to run ASP.NET or anything -- you still need to have the script on an ASPX page for the ASP.NET blocks to run. I'm assuming it is, right? So I'm not an ASP.NET expert -- are any of the other ASP.NET blocks being executed on your page? Is it just the ones in your inline JS that aren't getting replaced?

Jan 19, 2012 at 4:09 PM

"you still need to have the script on an ASPX page for the ASP.NET blocks to run. I'm assuming it is, right?'

Yes, the compiled / compressed version of the script is included in the asp.net pages <head></head> section. It's an external js file.


"Is it just the ones in your inline JS that aren't getting replaced?"

I didn't know the compiler could do inline js. I have setup everything as external js file that compiles into a minified js file. How do you run inline js through ajaxmin?



Jan 19, 2012 at 5:33 PM

That's why I asked what your set up was like; by "on an ASPX page," I meant inline on the page, not as external files. Some people have even modified their build process to minify external JS file and then inject them into the "built" ASPX pages at specific points (we do something similar at MSN). IIS won't run ASP.NET scripts within external JS files by default, although I'd be willing to bet someone knows how to turn that kind of processing on if you need it. I found this on the web: http://stackoverflow.com/questions/1343983/processing-asp-net-expressions-inside-an-external-javascript-file. Sounds like one thing to try might be to rename your external JS file to have an ASPX extension, then IIS would process it as an ASP.NET page. But that seems a little funky to me. And you might have to have a little ASP.NET block at the top of the file that sets your content type to whatever MIME type JS files are normally sent as.

For now you can't run an ASPX page though AjaxMin and have it just minify the inline JS. Would be nice, though.... Hmmm.... I bet I could do that relatively easily: open a file, read for "<script ... >", output everything up to and including that opening tag, find the next closing </script> tag, minify everything inbetween and send to the output file, then repeat from the closing tag until you get to the end of the file. I could even do the same thing for "<style...>" elements (but minify the CSS). Would that help you with your issue? Would it be possible to put your ASP.NET-infused inline in your ASPX files, then run a build step that crunches all the inline script and stylesheets into a "built" output ASPX file? Again, not sure how your project or build process is set up.

Jan 19, 2012 at 8:01 PM
Edited Jan 19, 2012 at 8:01 PM

I'm a bit confused now... How is the -aspnet switch supposed to be used? If the ajaxminify can't do inline JS, and asp.net can't process <% %> tags in external js files how do you use it?

I really don't have any sort of Build process in place . This is kind of a 1 man operation :) That is a pretty good technique for extracting inline js, but it's probably overkill for this project.


Jan 19, 2012 at 8:38 PM

If you don't use the -aspnet:1 switch, AjaxMin uses its default parsing behavior, and it will not recognize ASP.NET blocks in your code, nor do anything special with them; it just treats the input code as normal JavaScript or CSS syntax and will likely throw a whole bunch of JavaScript/CSS errors when an ASP.NET block is encountered. When you use the -aspnet:1 switch, then AjaxMin alters its parser syntax and performs a lot of extra parsing to recognize ASP.NET blocks and do special things with them. For instance, without the switch, this JavaScript code will throw a bunch of errors, since it's not, strictly speaking, proper JavaScript syntax:

var a = <%= Request["foo"] %>;

It hits that opening < character, thinks it's a less-than operator, but there wasn't a preceding expression for the comparison, then it encounters a modulo operator, etc. But with the -aspnet:1 switch specified, the parser recognizes that the right-hand expression contains an ASP.NET block delimited with <%= and %>, and treats that block as if it were a JavaScript identifier (in this particular case). Because most people who use the tool just want to parse regular JavaScript without any embedded ASP.NET blocks and the extra ASP.NET processing can slow things down, it doesn't do any of that extra processing by default; you have to turn it all on first, and that's what the switch is for.

Now, what you do with your JavaScript (whether or not it contains embedded ASP.NET code) is entirely up to you; AjaxMin makes no assumptions about your build processes or IIS (or Apache, or any other web server software) settings. For instance, at MSN, we have a very complex build process and content-publishing system in which JavaScript nuggets get assembled into various targets, and those targets might be used to create external JS files, and/or they might be used to create inline JavaScript on our XHTML pages. AjaxMin is used to minify the JavaScript, but it doesn't handle anything beyond the minification of the JavaScript -- that is up to whatever build system and/or publishing system you may be using. With AjaxMin, we are not attempting to create an entire build and content-publishing system; we're just making a tool that minifies JavaScript and CSS, which can then be integrated into whatever system you may be using. We added ASP.NET support because some of our users needed it.

I'd love to be able to give you some examples right now of how you can take your JavaScript and embed it into your ASP.NET page inline and thereby start taking advantage of the ASP.NET processing within your JS, but I'm not really an ASP.NET developer, and I haven't actually used that functionality myself. This would be a great point at which someone in the AjaxMin community who has used it might step up and demonstrate what they've been doing. If no one steps up, maybe you can look at various discussion or issue postings on this site related to ASP.NET processing and contact one of those individuals -- it's probably a good bet they are using it in their applications somehow.

However, one idea that was in that link I previously mentioned suggested putting an ASPX extension on your external JS files that require ASP.NET processing. So taking the original command-line you posted earlier, try this one:
    ajaxmin -JS -clobber chat\config.js -out scripts\optimized\chat.js.aspx -aspnet:1
And change your ASPX file to try downloading scripts\optimized\chat.js.aspx instead. of just scripts\optimized\chat.js That web page seemed to indicate that it would cause your otherwise-static JS file to be processed as ASP.NET code. I haven't tried this myself, though, and I don't want to pretend to sound like I know how to write ASP.NET applications, so your mileage may vary.

Jan 21, 2012 at 5:52 PM

Thanks for the detailed explanation Ronlo, much appreciated! I'm just wondering if I have been using the switch in the wrong spot... For example you posted this command

    ajaxmin -JS -clobber chat\config.js -out scripts\optimized\chat.js.aspx -aspnet:1

When I run the optimization script my command is

    ajaxmin -JS -clobber -aspnet:1 chat\config.js -out scripts\optimized\chat.js.


I'll give that a try and let you know how that goes.

Jan 24, 2012 at 5:25 PM

Well I gave it a try and same result... I'm thinking this feature just doesn't work, so I'm going to leave those bits of JS in the page.

Feb 10, 2012 at 9:22 PM

The -aspnet:1 switch only tells AjaxMin that the input script contains ASP.NET syntax that must be preserved in the output. It doesn't execute any server-side code for you. Also, yes: the order of parameters in this case is irrelevant, as you have observed. I only highlighted it because you didn't have it in your original post of the command-line you were using at the top of the page.

Sorry I couldn't help you. I've explained what the -aspnet:1 switch does to the best of my ability, but it isn't intended to do what it sounds like you want it to do.