Solved

How can I run a Javascript function inside ASP.net C#?

Posted on 2015-01-12
11
353 Views
Last Modified: 2015-01-23
I'm planning a part of an already existing site (asp.net/c#), where the customer can add things to a shopping basket, but there are rules to what can be put in the shopping basket. If one thing is already in, another item can not be put in. Some items can only be bought, if the customer is younger than a certain age. Prices vary on customer private address. This set of rules, I'd want to code in Javascript, since I can make the browser come up with error messages immediately, when the customer tries something which is not allowed. But, when the customer needs to check out his shopping basket, I would like to do a final check on the items, but this time on the server. (In case the customer changed the javascript code, or the json string that is actually the shopping basket). How can I run the same .js snippet on the server?

I could do it the other way around, making the rules in a C# snippet. Then the client would need to ask about everything in an Ajax call. I would like to minimize the traffic to the web server, so it will only download the whole "store" as a complete web application, and only at the checkout, the server will need to checkout and handle the shopping basket.

Please comment on this.
0
Comment
Question by:lefodnes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 40544498
The obvious solution is to eliminate/hide/mask those things the shopper can't buy from your catalog.  I would get annoyed if I kept putting things into a basket only to find out I wasn't eligible to purchase them to begin with.  Why show me a product I can't buy?

(That said, I'm sure this is do-able via javascript, but I'm not the guy to tell you how.  Good luck!)
0
 

Author Comment

by:lefodnes
ID: 40544661
That is why I need the rule set. To hide things that are not available after the first item is chosen. This web shop is wizard driven, so the rule set is used by the wizard to know what to ask for next.
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40545410
You can not execute JS in your server code, but something that you could do is to run after the your page is executed in the server:
Alternative 1: Pure JS solution, use jquery $( document ).ready
Alternative 2: If for what ever reason you need to associate this JS code to your server code use ClientScriptManager.RegisterStartupScript

If more help needed please specify OS/VS/ASP.NET version/Project type.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40545431
No, you can't run the javascript validation on the server side ( which is a C# application, right? )

Personally i don't like business rules in the UI module, so what i do is filter the products the client can't buy before sending them to the browser, put simple rules of excluded products in the javascript code and then do a final check in the server side at checkout.

Best regards
0
 

Author Comment

by:lefodnes
ID: 40545540
I'm not closer to a solution, I'm afraid. Isn't there some ActiveX object that could read a js file on the server and execute a function in it? The only reason I want js on the server, is that I can reuse the same code on both the server and the clients. Otherwise, I could just have two equal rulesets, one in c# and one in JavaScript. But it's better to not have to maintain two "equal" functions.

As for the business logic separated from the ui module, I agree, but I would like to take it further, and let the ui module use a _copy_ of the business logic to avoid checking with the server while shopping. Anyway, doing this by Ajax would be the next best solution, as I see it.
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40545571
No, you can't process the javascript in the server side. With an Ajax solution you gain nothing because the server-side is the same.

Check Node.js but i feel this is an overly complex scenario.
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40545627
Duplicate code is not good idea for maintenance purposes. If my previous post alternatives are not applicable to your project, then you should add C# logic in a  web method and called using JQuery post. I know it uses the server but hopefully the information you require to send is not that big.
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40546676
I would like to minimize the traffic to the web server
This is a client requirement or a self imposed one, these days every web application makes a heavy use of ajax calls.

@Miguel i agree that duplicate code is not a good idea, but in different platforms (browser/server, javascript/C#) you will  have some duplicated code but this must be the minimun necessary and TESTS must be in place.
0
 

Assisted Solution

by:lefodnes
lefodnes earned 0 total points
ID: 40546720
It's a self imposed requirement. I'm also running the servers. This web application gets up to 2-5000 unique users per minute, BUT only for a duration of 3-4 minutes. After that, it's half a year until the next time. It's a ticket web shop for a sporting event.

I've found "ClearScript", "JSlint.NET" and a few other alternatives now, after following the node.js suggestion, seeing that node.js works on top of Google Chrome V8 JS engine. Don't know if it is going to work. Trying "ClearScript" first, to see if it can run js-code inside c# ASP.net
0
 
LVL 10

Accepted Solution

by:
Walter Padrón earned 500 total points
ID: 40547005
In solutions like ClearScript,  you loose the compiler feedback because the code are strings, you can't share the code with the client and IMHO debugging will be a nightmare.

JsLint.Net is a wrapper for JsLint, is a code quality tool not a javascript engine.
0
 

Author Closing Comment

by:lefodnes
ID: 40566028
I had to come up with directions to ClearScript myself. Still not tested, but EE wants me to reply to this question.
0

Featured Post

Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question