0

I'm building a plain old HTML website that I'm putting a little Javascript into. Out of curiosity, I've been considering using other languages to accomplish the tasks that I want to achieve using Javascript, which include locating (and ordering) objects by date and tag variables.

Is it at all possible to incorporate C# in my JS code, referencing a library located somewhere in the site's directory? Would it theoretically be possible, or would I essentially just be translating the C# into the equivalent Javascript?

3
  • you can make ajax calls to a web api, but your c# code won't run in a javascript context Commented Jul 17, 2016 at 17:18
  • @DeblatonJean-Philippe So I'd essentially have to host the entire C# library somewhere as an API, then use some complicated referencing to make it have even a chance of working? Commented Jul 17, 2016 at 17:20
  • See stackoverflow.com/questions/36491385/… , stackoverflow.com/questions/37934181/… Commented Jul 17, 2016 at 17:21

1 Answer 1

2

Sorry, you can't. Browsers can only run Javascript code. There are other languages (coffeescript, typescript, etc) that "compile" to javascript, but in the end the only valid language for client side scripting is Javascript.

There are some "C# to javascript" translators, however that only gives you a similar syntax, but not the wide standard library that .NET Framework comes with.

What you can do however is run your C# code on server side (you can run ANYTHING there, since it's completely under your control) and then use AJAX to call it. This is known as a "web service" and it's a pretty standard practice.

Sign up to request clarification or add additional context in comments.

4 Comments

So, assuming I built a small application that runs server-side, I could allow calls to it from Javascript, returning valid variables? Essentially, a glorified API with it's own executable context?
Yup. And, since it's on the server side, nobody sees what's going on. Good for security - you can do all kinds of stuff, connect to DB's, check credentials, etc - and nobody could possibly get access to anything that you didn't allow them to. (But be careful - make a mistake, and it's a security hole)
Ah. So I'd be spending more time replicating what is already provided by existing templates.
Huh? What do you mean?

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.