AJAX URL Routing Issue in IIS with Virtual Directory

I got a .NET MVC3 project that was just deployed to the actual server inside a virtual directory. Let me call him VirtDir . In my JavaScript files, all the URLs for AJAX are defined as follows:

 "/Home/Save/" 

which worked fine locally.

Actually this first "/" kills the virtual directory, so I get

"/Home/Save/" instead of "/VirtDir/Home/Save/" , which obviously fails.

If I delete the first "/", then the events break locally, as a result of which a specific URL is added to the URL of the current page: if you are on the "/Home/Index" page, the AJAX URL points to

"/Home/Index/Home/Save" .

My JavaScript is in separate * .js files, so I cannot write them @Url.Content .

Does anyone know of a good solution to this problem?

+8
jquery ajax url-routing iis asp.net-mvc-3
source share
1 answer

You can add code to your layout file or to the main page, which writes the root directory to the JS variable before including the JS file. Then you can use this variable to create the path in your JS file.

In Razor:

 <script type="text/javascript"> var rootDir = "@Url.Content("~/")"; </script> <script src="@Url.Content("~/Scripts/MyScript.js")" type="text/javascript"></script> 

Then you can simply create your url in your file as follows:

 var myurl = rootDir + "Home/Save/"; 
+22
source share

All Articles