Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 472
Hairgami_Master Avatar asked Jun 06 '12 15:06

Hairgami_Master


2 Answers

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.

like image 60
mattmanser Avatar answered Oct 28 '22 13:10

mattmanser


I recommend you to use this:

url: '<%= ResolveClientUrl("~/Account/UserNameExists/")',

If you do it this way you'll avoid problems if you:

  • install the app in a virtual directory instead of the domain root
  • move your page to a different directory level in your app
  • use your service from a master page or user control, which can be instantiated in different pages, an thus directory levels

You can also expose a public property in your page/user control/master page, and use it from code in the same way, i.e:

  • code in the page/uc/master: public string ServiceUrl { get { return ResolveClientUrl("~/Account/UserNameExists/");}
  • code in .aspx: url: '<%= ServiceUrl',
like image 38
JotaBe Avatar answered Oct 28 '22 13:10

JotaBe