Newbie
Newbie

Reputation: 63

At the javascript, get value from C#

Can I perform something like this?

Situation I want to check the URL. if URL equal to http://sample.com, do this, otherwise, do that.

What I did:

In Web.config -

<add key="ServerURLCloud" value="sample.com" />

In C# -

public static string GetURL()
        {
            string[] url = ConfigurationManager.AppSettings["ServerURLCloud"];
            return url;
        }

In Javascript -

if(varURL.indexOf('@ClassName.GetURL()') > 0){
   urlToCall = 'sub.sample.com';
}else{
   urlToCall = 'sub.not-sample.com';
}

$ajax(
url = urlToCall,
data = .........
....
)

I tested it, it is working very well. But, I want to know, will it be any problem if:

  1. Internet connection slow

EDITED:

My Question

Is this practice (get Server side information at JavaScript) is good? Or bad?

Upvotes: 0

Views: 75

Answers (1)

Gareth Balfour
Gareth Balfour

Reputation: 176

i believe this code sample can be altered slightly to make it a little easier to maintain.

You could create a variable in your layout which could contain ConfigurationManager.AppSettings["ServerURLCloud"]

var siteSettings = {};
siteSettings.serverUrlCloud = '@ConfigurationManager.AppSettings["ServerURLCloud"]';
siteSettings.subSampleUrl = 'url';
siteSettings.subNotSampleUrl = '';

This site settings can hold anything useful as well (like base url etc)...

Also, try not to use magic strings in your code... instead, prefer to create variables/consts etc which hold these.

These changes wont impact the speed of your application but they will make it slightly easier to manage.

Also, the speed of the response from your ajax request is completely down to the executed code within that request, the length of the response and the internet connection speed... if the code is complex and doing a lot then it will naturally take longer. If the response is big, it will take longer to download. If the internet connection is slow, it will take longer to send the request and download the response.

Hope this helps

Upvotes: 1

Related Questions