Tuesday, July 5, 2011

ASP.NET Razor - HowTo



In this chapter you will learn how to add Razor code to a web page

Adding Razor Code

Remember the web page from previous chapter:
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Hello World Page</title>
</head>
<body>
    <h1>Hello World Page</h1>
    <p>Hello World Page</p>
</body>
</html>
Now add some Razor code to the example:
<!DOCTYPE html>

<html lang="en">
<head>
     <meta charset="utf-8" />
     <title>Hello World Page</title>
</head>
<body>
     <h1>Hello World Page</h1>
     <p>Hello World Page</p>
     <p>The time is @DateTime.Now</p>
</body>
</html>
The page contains ordinary HTML markup, with one addition: the @ marked Razor code.
The Razor code does all the work of determining the current time on the server and display it. (You can specify formatting options; this is just the default.)
Run the page. You now see the current date and time on the page:
WebMatrix

No comments:

Post a Comment