ASP.NET:使用HttpModule(給頁面添加頁頭和頁尾,重寫URL)


ylbtech-asp.net:內容簡介
技術與環境
操作系統: windows 語言類別: C#
thankyou: sunshine, 謝謝你的默默付出 課題: HttpModule
學習軟件: Microsoft Visaul Studio 2010
1.1.1,Init方法注冊事件 返回頂部

 

1.1.2,使用HttpModule(給頁面添加頁頭和頁尾) 返回頂部

 /App_Code/SimpleModule.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


    //<system.web>
    //    <httpModules>
    //        <add name="Text" type="SimpleModule"/>
    //  <!--<add name="UrlRewriter" type="UrlRewriter"/>-->
    //    </httpModules>
    
    //</system.web>
/// <summary>
///SimpeModule 的摘要說明
/// </summary>
public class SimpleModule:IHttpModule
{
    public SimpleModule()
    {
        //
        //TODO: 在此處添加構造函數邏輯
        //
    }

    #region IHttpModule 成員
    public void Dispose()
    {
    }
    public void Init(HttpApplication context)
    { 
        context.BeginRequest +=new EventHandler(context_BeginRequest);
        context.EndRequest +=new EventHandler(context_EndRequest);
    }
    void context_EndRequest(object sender, EventArgs e)
    {
        ((HttpApplication)sender).Response.Output.Write("<hr>頁尾");
    }
    void context_BeginRequest(object sender, EventArgs e)
    {
        ((HttpApplication)sender).Response.Output.Write("頁頭<hr>");
    }
    #endregion

}
1.1.3,URL重寫 返回頂部

 App_Code/UrlWrite.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///UrlRewriter 的摘要說明
/// </summary>
public class UrlRewriter:IHttpModule
{
    public UrlRewriter()
    {
        //
        //TODO: 在此處添加構造函數邏輯
        //
    }

    #region IHttpModule 成員
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest); ;
    }
    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpContext context = ((HttpApplication)sender).Context;
        string requestPath = context.Request.Path.ToLower();
        //判斷請求路徑是否為鮮花頁面
        if (requestPath.Contains("/webhttpmodule/flower/"))
        { 
            //找到路徑中最后一個"/"的位置
            int start = requestPath.LastIndexOf("/");
            //找到最后一個"."的位置
            int end = requestPath.LastIndexOf(".");
            string id = requestPath.Substring(start + 1, (end - start));
            context.RewritePath("~/flower/flower.aspx?id=" + id);
        }
    }
    #endregion
}

web.config

<?xml version="1.0"?>
<!--
  有關如何配置 ASP.NET 應用程序的詳細信息,請訪問
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <system.web>
        <httpModules>
            <add name="Text" type="SimpleModule"/>
      <!--<add name="UrlRewriter" type="UrlRewriter"/>-->
        </httpModules>
        <compilation debug="true" targetFramework="4.0"/>
    
    </system.web>
</configuration>

 

warn 作者:ylbtech
出處:http://ylbtech.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM