[CLOSED] MVC application absolute/relative urls in javascript ajax requests

  1. #1

    [CLOSED] MVC application absolute/relative urls in javascript ajax requests

    I have an MVC application where I need to make Ajax calls to various controller actions. What is the best way to handle the path to the controller actions relative to the host page where these actions are launched from? Does Ext.net have anything built in that I could use?

    For instance, I currently have the following call which I don't like and I want to change in order to be immune to the path of the parent page:

          Ext.Ajax.request({
            url: '../../Applications/GetApplicationData',
            method: 'POST',
            params: {
              'organizationId': this.selectedOrganizationId,
              'applicationId': this.selectedApplicationId,
              'storeRequestsInfo': Ext.encode(storeRequests)
    
            },
            success: function onSuccess(result, request)
            {
               ...
            },
            failure: function (result, request)
            {
              ....
            }
          });
    Instead of using '../../Applications/GetApplicationData' I would like to use ' ~/Applications/GetApplicationData' as I would normally use in .Net. However, Javascript doesn't know what ~ means, so I was hoping that perhaps Ext provides some javascript function that would know what to do with ~, something like:

    url: Ext.ResolveUrl(' ~/Applications/GetApplicationData')

    Thanks
    Last edited by Daniil; Jul 25, 2011 at 6:47 PM. Reason: [CLOSED]
  2. #2
    Hi,

    Is the following url not same?
    url: '/Applications/GetApplicationData'
    What url generates ASP.NET for '~/Applications/GetApplicationData'?
  3. #3
    The correct answer is: it depends.

    If the application is rooted at the server level, it is the same. But if it is not, then it's not the same.

    During development the web application runs off http://localhost:<some port number>. In this case using /Applications/GetApplicationData works. The ext.net mvc sample is rooted at the server level: http://mvc.ext.net/. /Applications/GetApplicationData would work in this case.

    But let's say you deploy your application on IIS7 in a specific application directory and you access it through: http://server/app_dir/... then /Applications/GetApplicationData is not the same as ~/Applications/GetApplicationData. In this case the first url points to /Applications/GetApplicationData (i.e. http://server/Applications/GetApplicationData which is not what I want) while the second one points to /app_dir/Applications/GetApplicationData ((i.e. http://server/app_dir/Applications/GetApplicationData which is what I want). When you use ~ you don't have to worry about the virtual directory name you deployed your app in. You can move it to a different virtual name and all the paths would still work.

    To conclude, my original question was whether there is some function in ext that I can use to resolve urls in JavaScript using the tilde notation for urls and that would still work if I deploy my app in a specific virtual folder.


    Thanks
  4. #4
    You can generate url at the server side and place it to the hidden field
    <ext:Hidden ID="UrlField" runat="server" Text="<%# GetRequiredUrl() %>" AutoDataBind="true"/>
    
    url : UrlField.getValue()
  5. #5
    Also, ResourceManager has appName property (client side)
    So, you can generate url as following
    function buildUrl(url){
         var appName = Ext.isEmpty(Ext.net.ResourceMgr.appName, false) ? "" : (Ext.net.ResourceMgr.appName + "/");
         return "/" + appName + url;
    }
    
    url : buildUrl("Controller/Action")
  6. #6
    This is what I was looking for.

    Thank you

Similar Threads

  1. [OPEN] [#129] Can Ajax requests send If-Modified-Since headers
    By anup in forum 1.x Legacy Premium Help
    Replies: 8
    Last Post: Jan 18, 2013, 4:26 AM
  2. Absolute/relative routes in MVC
    By Dominik in forum 1.x Help
    Replies: 2
    Last Post: Jun 21, 2010, 3:15 PM
  3. [CLOSED] Adding params as jsonData for jsin Ajax requests
    By r_honey in forum 1.x Legacy Premium Help
    Replies: 7
    Last Post: May 06, 2010, 7:31 AM
  4. Replies: 0
    Last Post: Jul 22, 2009, 4:40 AM
  5. Replies: 2
    Last Post: Jun 30, 2009, 4:03 PM

Tags for this Thread

Posting Permissions