0

I have a mvc3 web application which will be delopyed to an directory of a website. the address of the application seems like http://wwww.xx.com/aopo/. aopo is the name of the directory. After the deployement I find that the url in js file does not work any more. because I had write the js code with hard code url, just like

$.ajax({url:"/employee/getemployee"})

In that case the request will be sent to http://wwww.xx.com/employee/getemployee instead of http://wwww.xx.com/aopo/employee/getemployee, and 404 is returned. There is lots of code in js like that. I don't want to modify the js. Is there any simple approach to make it work? Can I rewrite the request in global.asax.cs?

3 Answers 3

3

I believe you'll need to change your url references to this:

$.ajax({url:"@Url.Action("getemplotee","employee")"})

If this is a script file you can write the correct URL into an element on the page and read that:

<div id='Main' data-url='@Url.Action("getemplotee","employee")' />

Then in your script:

$.ajax({url:$('#Main').data('url')})

Rgarding:

There is lots of code in js like that. I don't want to modify the js

I understand, but you need to use the proper helper methods to account for running under virtual directories, so I would recommending biting the bullet and just fixing it the right way.

1
  • yeah, I agree. this should be a normal solution.
    – Robin Xing
    Commented May 14, 2013 at 0:48
2

One not so clean solution might be,

Setting a url prefix in the layout(in a common place), like

<script type="text/javascript">
    @{
        string root = Url.Content("~/");
        if (root.EndsWith("/", StringComparison.Ordinal))
        {
            root = root.Substring(0, root.Length - 1);
        }
    }
    window.URL_PREFIX = '@root';
</script>

then use the prefix in the ajaxSend hook and change the url.

$(document).ajaxSend(function (event, jqxhr, settings) {
    if (window.URL_PREFIX && settings.url.indexOf(window.URL_PREFIX)) {
        settings.url = window.URL_PREFIX + settings.url;
    }
});

note: If $.ajax() or $.ajaxSetup() is called with the global option set to false, the .ajaxSend() method will not fire.

its not an ideal solution, but might be helpful in a tight situation.

hope this helps.

1
  • you are welcome, this solution will make the app working state while you start changing all $.ajax({url:"/employee/getemployee"}) to $.ajax({url: window.URL_PREFIX +"/employee/getemployee"}), and when all url are changed in the js files you can ditch the ajaxSend hook.
    – shakib
    Commented May 14, 2013 at 6:58
1

No, that's not possible. Since the request doesn't arrive in your application you can't access it and thus you can't rewrite it.

If you have control over the root site, you could use the IIS rewrite module to rewrite the URL to your website.

1
  • Then you will either have to adapt all your calls as mentioned by @asawyer or implement the fix by shakib. I would suggest cleaning your solution with the Url.Action methods. That will be the best solution in the long run.
    – Kenneth
    Commented May 14, 2013 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.