views:

71

answers:

2

Consider the need to $.post() to a slightly different URL structure in the Visual Studio Dev environment vs. deployed IIS Production or Test environment.

When deployed to the Test server, the app is running under a Virtual Directory in IIS. The URL will be something like:

Deployed

URL: http://myServer/myApplication/Area/Controller/Action

Using jQuery's .post(), I need to supply the string:

 $.post( "/myApplication/myArea/myController/myMethod"

Development

When in the Visual Studio environment

Cassini URL is: http://localhost:123/Area/Controller/Action

Using jQuery's .post(), I need to supply the string:

 $.post( "/myArea/myController/myMethod"

Question: How can I make both these use the same line of code, regardless of their deployed environment?

+2  A: 

The way I've done this is by generating the url from the RouteUrl method like so:

var url = "<%= Url.RouteUrl(new { area="myArea", controller = "controller", action = "actionmethod" }) %>";
$.post(url ...

As long as your routes are set up correctly, this will generate the appropriate Url.

Edit: Now works with areas without modification.

mc2thaH
A: 

Another (simpler?) implementation would be to setup a js variable of your application root:

<script type="text/javascript" >
    var globalAppPath = '<%= Request.ApplicationPath %>';
</script>

Then you can just append it to the beginning of any url request.

$.post( globalAppPath + "/myArea/myController/myMethod"

It'll work no matter where you put your web app.

Evildonald