Leo Wang
Leo Wang

Reputation: 31

Jquery ajax url when publishing asp.net mvc project on iis

When I use jQuery post function in my js file, like this:

$.post("/map/GetWindowedMapInfo", { 
    width: windowWidth * 2, 
    height: windowHeight * 2, 
    xCoord: winXCoord, 
    yCoord: winYCoord }, function (data) {...});

It work well in vs2010 developer server, but failed when publishing the ASP.NET MVC project on IIS, the associated controller function did not run anymore, because url was wrong. I have to use '<%= Url.Content("~/map/GetWindowedMapInfo") %>' to replace old url, but this can only work in the .aspx or .ascx file. Do you know any method to make a usable url in js file, not using <%=Url.Content(...) %> function, because I don't want to put my js file in aspx page.

Upvotes: 1

Views: 1538

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039418

You shouldn't be hardcoding urls like this:

$.post("/map/GetWindowedMapInfo", { 
    ...    
});

Instead you should use URL helpers:

$.post("@Url.Action("GetWindowedMapInfo", "map")", { 
    ...    
});

Upvotes: 1

Related Questions