Solved

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

Posted on 2015-01-12
11
311 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
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 33

Expert Comment

by:paulmacd
Comment Utility
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
Comment Utility
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 35

Expert Comment

by:Miguel Oz
Comment Utility
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
 
LVL 10

Expert Comment

by:Walter Padrón
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Expert Comment

by:Walter Padrón
Comment Utility
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 35

Expert Comment

by:Miguel Oz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Prologue It is often required to host multiple websites on a single instance of IIS, mostly in development environments instead of on production servers. I am sure it is not much a preferred solution on production servers but this is at least a pos…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now