I have a c# MVC project with some jQuery driven interface. Some actions are performed with a $.post(). Sometimes, the site would be deployed to the root of the domain (e.g. www.mydomain.com) and sometimes it would be deployed in a folder (e.g. www.mydomain.com/Super).
I want to make posts to controllers relative to the location of the view that the user is accessing. However, the user may access the view both with and without an ending /
, so i need to cater for both scenarios to avoid weird requests to www.mydomain.com/Super//
somecontroller.
Currently, i have a function that does the following:
function getlocation() {
var loc = "";
if (location.pathname != "/") {
loc = (window.location.href.match("/$")) ? window.location.href : window.location.href + "/";
}
return loc;
}
Ultimately, this looks pretty ugly to me. Besides, every time i want to make a post or insert a relative URL when parsing data, i have to insert the result of that function before the actual URL which contributes to tag soup.
What is the best way to approach this?
This is a case of me not asking the question correctly.
The problem as i had it is best solved using Url.Action()
method from the UrlHelper.