Archive for the ‘ASP.NET’ Category

  1. Hello, StringTemplate.NET World!

    Posted on 2月 13th, 2012 by cx20

    StringTemplate.NET

    StringTemplate.NET は Java 用のテンプレートエンジン「StringTemplate」の .NET 実装である。

    ソースコード(テンプレート)

    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p>Hello, $message$ World!</p>
      </body>
    </html>

    ソースコード(C#)

    using System;
    using System.IO;
    using System.Diagnostics;
    using Antlr3.ST;
    using Antlr3.ST.Language;
     
    public partial class _Hello : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                String template = Server.MapPath("templates");
                StringTemplateGroup group = new StringTemplateGroup("group", template);
                StringTemplate st = group.GetInstanceOf("hello");
                st.SetAttribute("message", "StringTemplate.NET");
                Response.Write(st);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
            }
        }
    }

    実行方法

    ディレクトリ構成
    /Hello               … 公開用フォルダ
        /Hello.aspx
        /Hello.aspx.cs
        /Web.config      … ASP.NET 構成ファイル
        /bin
             /Antlr3.Runtime.dll
             /Antlr3.StringTemplate.dll
        /templates      … テンプレート配置場所
              /hello.st
     
    1. テンプレートの配置
       templates
    2. ブラウザで表示
       http://localhost/Hello/Hello.aspx

    実行結果

    Hello, StringTemplate.NET World!
    
  2. Hello, NVelocity World!

    Posted on 2月 12th, 2012 by cx20

    NVelocity

    NVelocity は Java 用のテンプレートエンジン「Velocity」の .NET 実装である。

    ソースコード(テンプレート)

    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p>Hello, $message World!</p>
      </body>
    </html>

    ソースコード(C#)

    using System;
    using System.IO;
    using System.Diagnostics;
    using Commons.Collections;
    using NVelocity;
    using NVelocity.App;
    using NVelocity.Context;
     
    public partial class _Hello : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VelocityEngine velocity = new VelocityEngine();
            VelocityContext context = new VelocityContext();
            StringWriter sw = new StringWriter();
            try
            {
                String properties = Server.MapPath("nvelocity.properties");
                velocity.Init(properties);
                Template template = velocity.GetTemplate("hello.vm");
                context.Put("message", "NVelocity");
                template.Merge(context, sw);
                Response.Write(sw.GetStringBuilder().ToString());
            }
            catch( Exception ex )
            {
                Debug.WriteLine("Exception: " + ex.Message);
            }
        }
    }

    実行方法

    ディレクトリ構成
    /Hello               … 公開用フォルダ
        /Hello.aspx
        /Hello.aspx.cs
        /nvelocity.properties … NVelocity 設定ファイル
        /Web.config      … ASP.NET 構成ファイル
        /bin
             /NVelocity.dll
        /templates      … テンプレート配置場所
              /hello.vm
     
    1. テンプレートの配置
       templates
    2. nvelocity.properties の記載
    resource.loader = file
    file.resource.loader.class = NVelocity.Runtime.Resource.Loader.FileResourceLoader
    file.resource.loader.path = C:\home\edu\vs2010\vc10#\Hello\templates
    file.resource.loader.cache = true
    file.resource.loader.modificationCheckInterval = 2
    3. ブラウザで表示
       http://localhost/Hello/Hello.aspx

    実行結果

    Hello, NVelocity World!
    
  3. 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>

    ソースコード(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>

    上記 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> ");
            }
        }
    }

    実行方法

    1. IIS の公開フォルダ に配置
    2. ブラウザで表示
       http://localhost/doc/hello.cshtml … C# の場合
       http://localhost/doc/hello.vbhtml … VB の場合

    実行結果

    Hello, ASP.NET Razor World!
  4. Hello, ASP.NET World!

    Posted on 2月 10th, 2012 by cx20

    ASP.NET

    ASP.NET は、ASP の後継でマイクロソフトの Web サーバー(IIS)でプログラムを動作させる技術の一つである。
    既定の言語として Visual Basic(VB.NET)や C# が用いられるが、実際にはコンパイルされた .NET アセンブリが使用される為、JScript.NET や C++/CLI など .NET に対応した他の言語でも記述が可能である。

    ソースコード(Visual Basic)

    <%@ Page Language="VB" %>
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><% Response.Write( "Hello, ASP.NET World!" ) %></p>
      </body>
    </html>

    ソースコード(C#)

    <%@ Page Language="C#" %>
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><% Response.Write( "Hello, ASP.NET World!" ); %></p>
      </body>
    </html>

    ソースコード(JScript.NET)

    <%@ Page Language="JScript" %>
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><% Response.Write( "Hello, ASP.NET World!" ); %></p>
      </body>
    </html>

    上記コードは、以下の .NET のコードに相当する(以下の例は C# のケース)。実行時に .NET アセンブリにコンパイルされ実行される。

    ソースコード

     
    namespace ASP {
        using System.Web;
        using System.Text.RegularExpressions;
        using System.Web.Profile;
        using System.Web.UI.WebControls;
        using System.Web.Security;
        using System.Collections.Generic;
        using System.Collections.Specialized;
        using System;
        using System.Xml.Linq;
        using System.Collections;
        using System.Linq;
        using System.Web.UI;
        using System.Web.DynamicData;
        using System.Text;
        using System.Web.Caching;
        using System.Web.UI.HtmlControls;
        using System.Configuration;
        using System.Web.UI.WebControls.WebParts;
        using System.Web.SessionState;
        using System.ComponentModel.DataAnnotations;
     
        [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
        public class hello_aspx : global::System.Web.UI.Page, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
            private static bool @__initialized;
            private static object @__fileDependencies;
     
            [System.Diagnostics.DebuggerNonUserCodeAttribute()]
            public hello_aspx() {
                string[] dependencies;
                ((global::System.Web.UI.Page)(this)).AppRelativeVirtualPath = "~/hello.aspx";
                if ((global::ASP.hello_aspx.@__initialized == false)) {
                    dependencies = new string[1];
                    dependencies[0] = "~/hello.aspx";
                    global::ASP.hello_aspx.@__fileDependencies = this.GetWrappedFileDependencies(dependencies);
                    global::ASP.hello_aspx.@__initialized = true;
                }
                this.Server.ScriptTimeout = 30000000;
            }
     
            protected System.Web.Profile.DefaultProfile Profile {
                get {
                    return ((System.Web.Profile.DefaultProfile)(this.Context.Profile));
                }
            }
     
            protected override bool SupportAutoEvents {
                get {
                    return false;
                }
            }
     
            protected System.Web.HttpApplication ApplicationInstance {
                get {
                    return ((System.Web.HttpApplication)(this.Context.ApplicationInstance));
                }
            }
     
            [System.Diagnostics.DebuggerNonUserCodeAttribute()]
            private void @__BuildControlTree(hello_aspx @__ctrl) {
                this.InitializeCulture();
                @__ctrl.SetRenderMethodDelegate(new System.Web.UI.RenderMethod(this.@__Render__control1));
            }
     
            private void @__Render__control1(System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) {
                @__w.Write("rn<html>rn<head>rn<title>Hello, World!</title>rn</head>rn<body>rn<p>");
                Response.Write( "Hello, ASP.NET World!" );
                @__w.Write("</p>rn</body>rn</html> ");
            }
     
            [System.Diagnostics.DebuggerNonUserCodeAttribute()]
            protected override void FrameworkInitialize() {
                base.FrameworkInitialize();
                this.@__BuildControlTree(this);
                this.AddWrappedFileDependencies(global::ASP.hello_aspx.@__fileDependencies);
                this.Request.ValidateInput();
            }
        }
    }

    実行方法

    1. IIS の公開フォルダ に配置
    2. ブラウザで表示
       http://localhost/doc/hello.aspx

    実行結果

    Hello, ASP.NET World!