Search code examples
c#javascriptvisual-studio-2010web-config-transform

In Visual Studio C#, is there a way to apply Web Config Transforms to Javascript files?


In Visual Studio, I have some Javascript code on a site I'm developing. While I'm debugging I'm using the $ajax call to "localhost". When deployed, it will need to be the actual server:

$('#textInput_UserName').focusout(function () {
    var _username = $('#textInput_UserName').val();
    $.ajax({
        url: 'http://localhost:8809/Account/UserNameExists/',
        data: { username: _username },
        dataType: 'html',
});

When I publish, I need to transform that localhost to the actual domain:

$('#textInput_UserName').focusout(function () {
    var _username = $('#textInput_UserName').val();
    $.ajax({
        url: 'http://www.mydomain.com/Account/UserNameExists/',
        data: { username: _username },
        dataType: 'html',
});

Is there an easy/automatic way to do this, similar to the way Web Config transforms work?

Many thanks!


Solution

  • You don't, you just omit the host, the browser will fill this in for you, like this:

    $('#textInput_UserName').focusout(function () {
        var _username = $('#textInput_UserName').val();
        $.ajax({
            url: '/Account/UserNameExists/',
            data: { username: _username },
            dataType: 'html',
    });
    

    If you're actually talking about x-domain requests, which I doubt you are, then just set a global js site variable.