Hello, ASP.NET Razor World!
Posted on 2月 11th, 2012 by cx20
ASP.NET Razor
Razor は ASP.NET の新しい Web ページ記述構文である。
ASP や PHP のように HTML の中にスクリプトを埋め込むように記述できるという特徴がある。
実際の記述コードの言語としては、C# や Visual Basic(VB.NET)が用いられる。
実行環境として ASP.NET MVC 3 もしくは WebMatrix が必要。
ソースコード(C# による Razor 記法)
@{
var msg = "ASP.NET Razor";
}
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<p>Hello, @msg World!</p>
</body>
</html> |
@{
var msg = "ASP.NET Razor";
}
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<p>Hello, @msg World!</p>
</body>
</html>
ソースコード(Visual Basic による Razor 記法)
@Code
Dim msg = "ASP.NET Razor"
End Code
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<p>Hello, @msg World!</p>
</body>
</html> |
@Code
Dim msg = "ASP.NET Razor"
End Code
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<p>Hello, @msg World!</p>
</body>
</html>
上記 Razor 記法のコードは、以下の .NET のコードに相当する。実行時に .NET アセンブリにコンパイルされ実行される。
ソースコード(C#)
namespace ASP {
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Helpers;
using System.Web.Security;
using System.Web.UI;
using System.Web.WebPages;
using System.Web.WebPages.Html;
using WebMatrix.WebData;
using WebMatrix.Data;
public class _Page_hello_cshtml : System.Web.WebPages.WebPage {
public _Page_hello_cshtml() {
}
protected System.Web.HttpApplication ApplicationInstance {
get {
return ((System.Web.HttpApplication)(Context.ApplicationInstance));
}
}
public override void Execute() {
var msg = "ASP.NET Razor";
WriteLiteral("<html>rn<head>rn<title>Hello, World!</title>rn</body>rn<body>rn<p>Hello, ");
Write(msg);
WriteLiteral(" World!</p>rn</body>rn</html> ");
}
}
} |
namespace ASP {
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Helpers;
using System.Web.Security;
using System.Web.UI;
using System.Web.WebPages;
using System.Web.WebPages.Html;
using WebMatrix.WebData;
using WebMatrix.Data;
public class _Page_hello_cshtml : System.Web.WebPages.WebPage {
public _Page_hello_cshtml() {
}
protected System.Web.HttpApplication ApplicationInstance {
get {
return ((System.Web.HttpApplication)(Context.ApplicationInstance));
}
}
public override void Execute() {
var msg = "ASP.NET Razor";
WriteLiteral("<html>rn<head>rn<title>Hello, World!</title>rn</body>rn<body>rn<p>Hello, ");
Write(msg);
WriteLiteral(" World!</p>rn</body>rn</html> ");
}
}
}
実行方法
1. IIS の公開フォルダ に配置
2. ブラウザで表示
http://localhost/doc/hello.cshtml … C# の場合
http://localhost/doc/hello.vbhtml … VB の場合 |
1. IIS の公開フォルダ に配置
2. ブラウザで表示
http://localhost/doc/hello.cshtml … C# の場合
http://localhost/doc/hello.vbhtml … VB の場合
実行結果
Hello, ASP.NET Razor World! |
Hello, ASP.NET Razor World!
Tags: Razor
Categories: .NET, ASP.NET, C#, Razor, VB.NET