Phil Trelford's Array
POKE 36879, 255

Server-side JavaScript Razor style in 2012

October 26, 2012 10:37 by phil

Razor is a programming syntax for dynamic ASP.Net web pages released in 2011.

Here’s a C# syntax example from W3 Schools:

<!-- Single statement block -->
@{ var myMessage =    "Hello World"; }

<!-- Inline expression or variable -->
<p>The value of myMessage is: @myMessage</p> 

<!-- Multi-statement block -->
@{
var greeting = "Welcome to our site!";
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = greeting + " Today is: " + weekDay;
}
<p>The greeting is: @greetingMessage</p>

Here’s an example using server-side JavaScript:

<!-- Single statement block -->
<% var myMessage = "Hello World"; %>

<!-- Inline expression or variable -->
<p>The value of myMessage is: <%=myMessage%></p> 

<!-- Multi-statement block -->
<%
var greeting = "Welcome to our site!";
var year = new Date().getFullYear();
var greetingMessage = greeting + " The year is: " + year;
%>
<p>The greeting is: <%=greetingMessage%></p>

Eerily similar, eh?

The example above uses server-side JScript hosted in a classic ASP page which is lit up with IntelliSense in VS2012. Classic ASP and JScript were introduced in IIS way back in 1998. With version 3 in 2007 C# introduced the var keyword giving it a similar terse syntax to JavaScript. Nowadays you have the option of hosting classic ASP inside JavaScript itself with Node.js using osASP.


Tags:
Categories: JavaScript | C# | .Net
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading