国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

采用HttpModules來重寫URLs(實踐篇)

2024-07-21 02:16:20
字體:
供稿:網(wǎng)友
首先寫一個處理urls重寫的類,并且這個類必須繼承ihttphandler接口,以博客園的程序為例:

public class urlrewritemodule : system.web.ihttpmodule
{
public void init(httpapplication context)
{
context.beginrequest +=new eventhandler(context_beginrequest);
}

public void dispose()
{
}
}

urlrewritemodule類就是處理urls重寫的類,繼承ihttphandler接口,實現(xiàn)該接口的兩個方法,init和dispose。在init方法里注冊自己定義的方法,如上例所示:

content.beginrequest +=new eventhandler(content_beginrequest);

beginrequest是一個事件,在收到新的http請求時觸發(fā),content_beginrequest就是觸發(fā)時處理的方法。另外說明一點,httpmodules能注冊的方法還有很多,如:endrequest、error、disposed、presendrequestcontent等等。

在content_beginrequest方法中具體處理urls重寫的細(xì)節(jié),比如,將 http://www.cnblogs.com/rrooyy/archive/2004/10/24/56041.html 重寫為 http://www.cnblogs.com/archive.aspx?user=rrooyy&id=56041 (注:我沒有仔細(xì)看dudu的程序,這里只是舉例而已)。然后將重新生成的url用httpcontext.rewritepath()方法重寫即可,如下:

private void context_beginrequest(object sender, eventargs e)
{
httpcontext context = ((httpapplication)sender).context;
// 獲取舊的url
string url = context.request.path.tolower();
// 重新生成新的url
string newurl = ...; // 具體過程略
// 重寫url
context.rewritepath(newurl);
}

提醒:newurl的格式不是http://www.infotouch.com/user/archive.aspx,而是從當(dāng)前應(yīng)用程序根目錄算起的絕對路徑,如:user/archive.aspx,這一點請?zhí)貏e注意。

最后要web.config中注冊重寫urls的類,格式如下:

<httpmodules>
<add type="classname,assemblyname" name="modulename"/>
<remove name="modulename"/>
<clear />
</httpmodules>

采用<add>標(biāo)簽可以注冊一個類;<remove>可以移除某個類,如果某個子目錄不希望繼承父目錄的某個http module注冊,就需要使用這個標(biāo)簽;<clear />可以移除所有的http module注冊。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 广西| 秭归县| 江山市| 蒲江县| 东乡县| 额济纳旗| 通辽市| 浏阳市| 丘北县| 大安市| 札达县| 道真| 姚安县| 镇坪县| 宁陕县| 吉水县| 青田县| 景宁| 富平县| 辽宁省| 长乐市| 开远市| 沾化县| 安溪县| 偃师市| 修文县| 黑水县| 湄潭县| 报价| 客服| 专栏| 内乡县| 崇仁县| 北川| 武城县| 利辛县| 门源| 西乌珠穆沁旗| 乐山市| 昭觉县| 兴文县|