Show of hands, how many of you are using jqGrid?  Alright, now how many of you have tried to run your ASP.NET MVC application in a configuration where the root of the application isn’t “/”? Alright, now raise your hand if you are having to edit jquery.jqGrid.js when you move the application to the alternate configuration? 

NOTE: I’m assuming that your development environment runs with the application at the root of WebDev.WebServer.exe; if not, simply swap the assumptions, and I bet you still have the same problem I’m about to describe.

If your hand is not up, congratulations, you are doing something correctly (I think).  If your hand is up, you have an extra step in your deployment process.  Deployment should be as simple and as automated as possible, so needing to change a path in a JavaScript file is a Bad Thing.  Until today, our application required JavaScript changes when we moved it from development (where it runs at the root of the site) to staging (where it runs in a folder).  I took the time to fix it today.  Here’s how I did it.

First, open up jquery.jqGrid.js and find the line where “pathtojsfiles” is defined.  Delete it.  Add a parameter of the same name (pathtojsfiles) to the jqGridInclude function, like so:

   1: function jqGridInclude(pathtojsfiles)
   2: {
   3: ...
   4: }

Next, jump to the end of the file, and remove this line:

   1: jqGridInclude();

Now, in your view markup, add a new script block after you’ve included jquery.jqGrid.js:

   1: <script type='text/javascript'>
   2: jqGridInclude('<%=Url.Content("~/Content/path/to/JQGrid/js/")%>');
   3: </script>

What did we do?  We modified jqGridInclude to take the base path as a parameter, and we changed it to not be called automatically when the file is included.  Instead, we call it explicitly and use Url.Content to get the correct path to jqGrid’s JavaScript.

The down-side to this approach is that you’ve made (minor) modifications to jqGrid files, so you will need to re-apply this fix if you upgrade to a new release.  But, that’s a small price to pay for eliminating a step from your deployment process.