Search code examples
jqueryurliisasp.net-mvc-5

Different URL path for Visual Studio and IIS


How to set a URL in vanilla JavaScript or with jQuery when I deploy an application in IIS?

In Visual Studio, this URL works (without path):

url: '/street/details/'

But IIS needs this URL:

url: '**/utca**/street/details/'.

I woukd like to modify the URLs in every JavaScript file automatically.


Solution

  • If you're using asp.net-mvc-5 then you shouldn't be using any hardcoded paths.

    You should be using @Html.Action or @Url.Action or equivalents. This allows you to move things at will / per-environment.

    If you really must use paths directly, eg in a .js file where you can't use server-side and don't want to go to the hassle of passing the url either as a parameter or as a data- attribute, the easiest method is to set up a global (or namespaced) variable to your root path, eg place this at the <head> of your _layout.cshtml:

    <script type='text/javascript'>
        var mynamespace = mynamespace || {}; 
        mynamespace.rootPath = '@Url.Content("~")';
    </script>
    

    then your script can use this whereever needed, eg:

    $.ajax({
        url: mynamespace.rootPath + 'street/details/',