天天看點

*.ashx檔案

.ashx 檔案用于寫web handler的。其實就是帶HTML和C#的混合檔案。當然你完全可以用.aspx 的檔案字尾。使用.ashx 可以讓你專注于程式設計而不用管相關的WEB技術。.ashx必須包含IsReusable. 如下例所示

<% @ webhandler language="C#" class="AverageHandler" %>

using System;

using System.Web;

public class AverageHandler : IHttpHandler

{

public bool IsReusable

{ get { return true; } }

public void ProcessRequest(HttpContext ctx)

ctx.Response.Write("hello");

}

}

.ashx比.aspx的好處在與不用多一個html   

*ashx檔案

using System.Web

*ashx檔案

public sealed class TextBuilder : IHttpHandler

*ashx檔案
*ashx檔案
*ashx檔案

{

*ashx檔案

    public void ProcessRequest(HttpContext context)

*ashx檔案
*ashx檔案
*ashx檔案
*ashx檔案

        context.Response.ClearContent();

*ashx檔案

        context.Response.ContentType = "text/plain";

*ashx檔案

        context.Response.Write("Hello World");

*ashx檔案

        context.Response.End();

*ashx檔案

    }

*ashx檔案
*ashx檔案

    public bool IsReusable

*ashx檔案
*ashx檔案
*ashx檔案
*ashx檔案
*ashx檔案

        get 

*ashx檔案

{ return true; }

*ashx檔案
*ashx檔案
*ashx檔案
*ashx檔案

<%

*ashx檔案

@ WebHandler language="C#" Class="MyNamespace.TextBuilder" codebehind="TextBuilder.ashx.cs" %>