asp.net URL重寫簡(jiǎn)化版 速學(xué)URL重寫
更新時(shí)間:2010年01月08日 23:23:05 作者:
asp.net URL重寫(URLRewriter)簡(jiǎn)化版 。速學(xué)URL重寫
在 asp.net 里實(shí)現(xiàn) URL重寫(URLRewriter)的一個(gè)最簡(jiǎn)單的方法。
參考了 (作者 Scott Mitchell 翻譯:Janssen )的大作,雖然沒有完全看明白,但是也照貓畫虎地做了一個(gè),頗有“成就”感。寫出來(lái)分享一下。
原作里講了很多的原理,這里就不說了(其實(shí)我也不懂)。這里就寫操作過程吧。目的是實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的能實(shí)現(xiàn) URL重寫 的程序。
1、需要設(shè)置一下IIS里的站點(diǎn)屬性。
2、修改web.config的內(nèi)容。
<system.web>
<httpHandlers>
<add verb="*" path="*.zhtml" type="ZDIL.URLRewriter.RewriterFactoryHandler, ZDILURLRewriter" />
</httpHandlers>
</system.web>
其中*.zhtml 就是地址欄里面寫的網(wǎng)頁(yè)的擴(kuò)展名,就是給用戶看的,這個(gè)可以隨意改(但是要符合擴(kuò)展名的規(guī)則?。.?dāng)然要和第一步里面的設(shè)置相一致才行。
3、寫一個(gè)類。
代碼
using System;
using System.IO;
using System.Web;
using System.Web.UI;
namespace ZDIL.URLRewriter
{
/**//// <summary>
/// URL重寫
/// </summary>
public class RewriterFactoryHandler : IHttpHandlerFactory
{
/**//// <summary>
/// GetHandler is executed by the ASP.NET pipeline after the associated HttpModules have run. The job of
/// GetHandler is to return an instance of an HttpHandler that can process the page.
/// </summary>
/// <param name="context">The HttpContext for this request.</param>
/// <param name="requestType">The HTTP data transfer method (<b>GET</b> or <b>POST</b>)</param>
/// <param name="url">The RawUrl of the requested resource.</param>
/// <param name="pathTranslated">The physical path to the requested resource.</param>
/// <returns>An instance that implements IHttpHandler; specifically, an HttpHandler instance returned
/// by the <b>PageParser</b> class, which is the same class that the default ASP.NET PageHandlerFactory delegates
/// to.</returns>
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
string sendToUrl = url; //地址欄里面的地址
string filePath = pathTranslated;
string sendToURLString = "/web/index.aspx"; //真正要訪問的頁(yè)面
string queryString = ""; //參數(shù)。比如 ?id=123
filePath = context.Server.MapPath(sendToURLString); //物理地址
//這句最重要了。轉(zhuǎn)向了。
context.RewritePath(sendToURLString, String.Empty, queryString);
return PageParser.GetCompiledPageInstance(url, filePath, context);
}
public virtual void ReleaseHandler(IHttpHandler handler)
{
}
}
}
這個(gè)類呢,要寫在一個(gè)單獨(dú)的項(xiàng)目里面,然后編譯成 ZDILURLRewriter.DLL文件。(注意文件名,寫錯(cuò)了就不能正常運(yùn)行了)。
4、完成了。
打開IE ,在地址欄里輸入 http://.../1.zhtml。
瀏覽者看到是一個(gè)靜態(tài)頁(yè)的地址,但是實(shí)際上訪問的卻是 /web/index.aspx 這個(gè)動(dòng)態(tài)網(wǎng)頁(yè)。
怎么樣簡(jiǎn)單吧。
當(dāng)然了,這個(gè)是最簡(jiǎn)單的,簡(jiǎn)單到了“不能用”的地步了。因?yàn)樗麜?huì)把所有的 *.zhtml 的訪問都“重寫”到 /web/index.aspx 。
至于把什么樣的網(wǎng)頁(yè)重寫到哪個(gè)網(wǎng)頁(yè),這里就不介紹了(這里只講方法,不講實(shí)現(xiàn)的細(xì)節(jié))。
方法很多了,原作是通過正則來(lái)匹配的,我是通過 string sendToUrl = url; 來(lái)判斷的。
其他的就看你們的需要了。
參考了 (作者 Scott Mitchell 翻譯:Janssen )的大作,雖然沒有完全看明白,但是也照貓畫虎地做了一個(gè),頗有“成就”感。寫出來(lái)分享一下。
原作里講了很多的原理,這里就不說了(其實(shí)我也不懂)。這里就寫操作過程吧。目的是實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的能實(shí)現(xiàn) URL重寫 的程序。
1、需要設(shè)置一下IIS里的站點(diǎn)屬性。
2、修改web.config的內(nèi)容。
復(fù)制代碼 代碼如下:
<system.web>
<httpHandlers>
<add verb="*" path="*.zhtml" type="ZDIL.URLRewriter.RewriterFactoryHandler, ZDILURLRewriter" />
</httpHandlers>
</system.web>
其中*.zhtml 就是地址欄里面寫的網(wǎng)頁(yè)的擴(kuò)展名,就是給用戶看的,這個(gè)可以隨意改(但是要符合擴(kuò)展名的規(guī)則?。.?dāng)然要和第一步里面的設(shè)置相一致才行。
3、寫一個(gè)類。
代碼
復(fù)制代碼 代碼如下:
using System;
using System.IO;
using System.Web;
using System.Web.UI;
namespace ZDIL.URLRewriter
{
/**//// <summary>
/// URL重寫
/// </summary>
public class RewriterFactoryHandler : IHttpHandlerFactory
{
/**//// <summary>
/// GetHandler is executed by the ASP.NET pipeline after the associated HttpModules have run. The job of
/// GetHandler is to return an instance of an HttpHandler that can process the page.
/// </summary>
/// <param name="context">The HttpContext for this request.</param>
/// <param name="requestType">The HTTP data transfer method (<b>GET</b> or <b>POST</b>)</param>
/// <param name="url">The RawUrl of the requested resource.</param>
/// <param name="pathTranslated">The physical path to the requested resource.</param>
/// <returns>An instance that implements IHttpHandler; specifically, an HttpHandler instance returned
/// by the <b>PageParser</b> class, which is the same class that the default ASP.NET PageHandlerFactory delegates
/// to.</returns>
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
string sendToUrl = url; //地址欄里面的地址
string filePath = pathTranslated;
string sendToURLString = "/web/index.aspx"; //真正要訪問的頁(yè)面
string queryString = ""; //參數(shù)。比如 ?id=123
filePath = context.Server.MapPath(sendToURLString); //物理地址
//這句最重要了。轉(zhuǎn)向了。
context.RewritePath(sendToURLString, String.Empty, queryString);
return PageParser.GetCompiledPageInstance(url, filePath, context);
}
public virtual void ReleaseHandler(IHttpHandler handler)
{
}
}
}
這個(gè)類呢,要寫在一個(gè)單獨(dú)的項(xiàng)目里面,然后編譯成 ZDILURLRewriter.DLL文件。(注意文件名,寫錯(cuò)了就不能正常運(yùn)行了)。
4、完成了。
打開IE ,在地址欄里輸入 http://.../1.zhtml。
瀏覽者看到是一個(gè)靜態(tài)頁(yè)的地址,但是實(shí)際上訪問的卻是 /web/index.aspx 這個(gè)動(dòng)態(tài)網(wǎng)頁(yè)。
怎么樣簡(jiǎn)單吧。
當(dāng)然了,這個(gè)是最簡(jiǎn)單的,簡(jiǎn)單到了“不能用”的地步了。因?yàn)樗麜?huì)把所有的 *.zhtml 的訪問都“重寫”到 /web/index.aspx 。
至于把什么樣的網(wǎng)頁(yè)重寫到哪個(gè)網(wǎng)頁(yè),這里就不介紹了(這里只講方法,不講實(shí)現(xiàn)的細(xì)節(jié))。
方法很多了,原作是通過正則來(lái)匹配的,我是通過 string sendToUrl = url; 來(lái)判斷的。
其他的就看你們的需要了。
您可能感興趣的文章:
- asp.net不用設(shè)置iis實(shí)現(xiàn)url重寫 類似偽靜態(tài)路由
- ASP.NET中獲取URL重寫前的原始地址詳解
- URL重寫及干掉ASP.NET試圖狀態(tài)的實(shí)現(xiàn)方法
- 一個(gè)完整的ASP.NET 2.0 URL重寫方案[翻譯]
- asp.net url重寫淺談
- asp.net下實(shí)現(xiàn)URL重寫技術(shù)的代碼
- asp.net 2.0 中的URL重寫以及urlMappings問題
- asp.net下用url重寫URLReWriter實(shí)現(xiàn)任意二級(jí)域名的方法
- Asp.Net URL重寫的具體實(shí)現(xiàn)
相關(guān)文章
Community Server專題一:概述Community Server
Community Server專題一:概述Community Server...2007-03-03asp.net獲得數(shù)據(jù)控件事件索引并獲取其中值總結(jié)
asp.net獲得數(shù)據(jù)控件事件索引并獲取其中值總結(jié),需要的朋友可以參考下。2011-11-11asp.net下Oracle,SQL Server,Access萬(wàn)能數(shù)據(jù)庫(kù)通用類
Oracle,SQL Server,Access萬(wàn)能數(shù)據(jù)庫(kù)通用類!,使用asp.net開發(fā)多數(shù)據(jù)庫(kù)系統(tǒng)的朋友可以參考下。2010-10-10淺談.Net Core 認(rèn)證系統(tǒng)源碼解析
這篇文章主要介紹了淺談.Net Core 認(rèn)證系統(tǒng)源碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12MVC使用T4模板生成其他類的具體實(shí)現(xiàn)學(xué)習(xí)筆記2
這篇文章主要為大家詳細(xì)介紹了MVC使用T4模板生成其他類的具體實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09