views:

50

answers:

2

I'm using Asp.NET MVC and have a huge dependency on JQuery throughout my entire application (not sure if that's important here or not). I'm trying to build a set of js library files to go along side my views. The problem that I'm faced with is that I need a way to manage the server side location of my files and action links for all of my image maps and ajax calls.

For now, I'm stuck putting all of my scripting code inside each aspx pages so that I can use the server side scripting tags like this:

    $("#StartDate").datepicker({
        changeMonth: true,
        changeYear: true,
        showOn: 'button',
        buttonImage: '<%= Url.Content("~/Content") %>/Images/calendar.png',
        buttonImageOnly: true
    });

and

    function fillDates(periodType, periodOffset)
    {
      $.ajax({
        url: '<%= Url.Action("GetDates", "Tracking") %>',
        dataType: "json",
        data: {"pt": periodType, "po": periodOffset},
        success: function(result){
            if (result.Status == "Success")
            {
                $("#StartDate").val(result.StartDate);
                $("#EndDate").val(result.EndDate);
            }
            else
            {
                alert(result.Status + ": " + result.Message);
            }
        }
      });        
    }

I'd like to move this code to a js file. How would I do this and still be able to map to the root directory of my application.

+3  A: 

You can put the base url in your view, and reference it from your js files

// site.master
var appPath = <%=ResolveUrl("~/")%>;

// *.js
function dosomething()
{ 
    var src = appPath + "content/images/test.png";
    var action = appPath + "controller/action";
}

Note: this method requires that you write your action urls manually instead of calling Url.Action().

Joel Potter
I've thought of this as well, but it would require that I manually set the appPath in every view. I'm looking for a way to only reference the script file, with no extra code. Perhaps it's simply not possible though?? Thanks for your idea.
Luc
Do you know have a view master where you put your most basic functionality and layout?
Joel Potter
Yes I do, however the js files that I'm wanting to build will all have these same parameter names and values. I think I'm going to resort to building my own http handler (ie. jspx) so that I can keep my parameters logical to the functions and still have access to Url.Action / Url.Content / etc... as needed. Thanks again!
Luc
+1  A: 

Though I've never tried it, you could map .js files to the ASP.NET handler and process them when they are served. How you do that mapping depends on the version of IIS and you'd probably use an HTTPModule to do the processing.

jarrett
no need, you can safely do **<script src="something.aspx"....>**. that said, joel potter got it right, IMHO.
Nir Gavish
Thanks guys. I think I'm going to take this approach. However, instead of using a standard aspx extenstion as suggested by Nir Gavish, I'll create a jspx extension so I can keep it cleanly separated in my solution. Good stuff.
Luc