locked
Remove application path for my JS file RRS feed

  • Question

  • User2108892867 posted

    Hello everyone, I have this situation. Here is my code: 

      events: "../../../../service/mygenerichandler.ashx",

    As you can see, I am calling my generic handler using relative path. Now I want to make it a bit more flexible so it doesn't matter where my aspx file is, I can always call the generic handler. I can achieve this changing it to:

     events: "/service/mygenerichandler.ashx",

    So this will find the file relative to the root directory. It is working fine after I published my site cause the file is found at www.example.com/service/mygenerichandler.ashx. However, in my testing environment, the file could not be found cause Visual Studio automatically added the application name to the url. So it will be something like: http://localhost:22213/MySite2. So the file is actually located at http://localhost:22213/MySite2/service/mygenerichandler.ashx. Meanwhile the javascript was trying to locate the file at http://localhost:22213/service/mygenerichandler.ashx

    Is there an easy way to fix this so I don't need to go and change my js file every time? I hope it makes sense.

    Thanks for your help. 

    Monday, March 7, 2016 3:02 AM

Answers

  • User2108892867 posted

    Hello Bruce, thanks for the reply. My project is not an MVC project. It's a website. I did some research and found this:

     var _location = document.location.toString();
        var applicationNameIndex = _location.indexOf('/', _location.indexOf('://') + 3);
        var applicationName = _location.substring(0, applicationNameIndex) + '/';
        var webFolderIndex = _location.indexOf('/', _location.indexOf(applicationName) + applicationName.length);
        var webFolderFullPath = _location.substring(0, webFolderIndex);

    and my event will become something like this:

      events: webFolderFullPath + "/service/mygenerichandler.ashx",

    It's working fine on my location machine but when I published it. Then it's not working. All I want is when it is hosted in production environment, I want webFolderFullPath to be "" and when it is my local machine, the webFolderFullPath should be "http://localhost/mytest".

    Thanks. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 8, 2016 10:03 PM
  • User-474980206 posted

    in webforms you use:

           <script>$root = "<%=Page.ResolveUrl("~/")%>";</script>
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 9, 2016 2:13 AM

All replies

  • User2108892867 posted

    Anybody please? Thanks. 

    Tuesday, March 8, 2016 1:07 AM
  • User61956409 posted

    Hi asplearning,

    events: "/service/mygenerichandler.ashx",

    Using the above path, you should make sure whether service folder is located under the Web site root.

    Besides, this article explained how to specify paths for resources, you could check it.

    https://msdn.microsoft.com/en-us/library/ms178116.aspx

    Best Regards,

    Fei Han

    Tuesday, March 8, 2016 2:57 AM
  • User-474980206 posted

    you should define a javascript variable for this. before including any scripts (sample is razor):

       <script>var $root="Url.Content("~/")";</script>
       <script src="~/js/myscript.js"></script>

    then in the script its:

    events: $root + "service/mygenerichandler.ashx",

    Tuesday, March 8, 2016 5:19 AM
  • User2108892867 posted

    Yes the service folder is in the website root directory. But the issue is Visual Studio add application name to the url, hence it could not be found. 

    Thanks. 

    Tuesday, March 8, 2016 10:49 AM
  • User2108892867 posted

    Hello Bruce, thanks for the reply. My project is not an MVC project. It's a website. I did some research and found this:

     var _location = document.location.toString();
        var applicationNameIndex = _location.indexOf('/', _location.indexOf('://') + 3);
        var applicationName = _location.substring(0, applicationNameIndex) + '/';
        var webFolderIndex = _location.indexOf('/', _location.indexOf(applicationName) + applicationName.length);
        var webFolderFullPath = _location.substring(0, webFolderIndex);

    and my event will become something like this:

      events: webFolderFullPath + "/service/mygenerichandler.ashx",

    It's working fine on my location machine but when I published it. Then it's not working. All I want is when it is hosted in production environment, I want webFolderFullPath to be "" and when it is my local machine, the webFolderFullPath should be "http://localhost/mytest".

    Thanks. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 8, 2016 10:03 PM
  • User-474980206 posted

    in webforms you use:

           <script>$root = "<%=Page.ResolveUrl("~/")%>";</script>
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 9, 2016 2:13 AM