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

首頁(yè) > 編程 > .NET > 正文

asp.net URL重寫(xiě)簡(jiǎn)化版 速學(xué)URL重寫(xiě)

2020-01-18 00:59:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
在 asp.net 里實(shí)現(xiàn) URL重寫(xiě)(URLRewriter)的一個(gè)最簡(jiǎn)單的方法。
參考了 (作者 Scott Mitchell 翻譯:Janssen )的大作,雖然沒(méi)有完全看明白,但是也照貓畫(huà)虎地做了一個(gè),頗有“成就”感。寫(xiě)出來(lái)分享一下。
原作里講了很多的原理,這里就不說(shuō)了(其實(shí)我也不懂)。這里就寫(xiě)操作過(guò)程吧。目的是實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的能實(shí)現(xiàn) URL重寫(xiě) 的程序。
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 就是地址欄里面寫(xiě)的網(wǎng)頁(yè)的擴(kuò)展名,就是給用戶看的,這個(gè)可以隨意改(但是要符合擴(kuò)展名的規(guī)則!)。當(dāng)然要和第一步里面的設(shè)置相一致才行。
3、寫(xiě)一個(gè)類(lèi)。

代碼
復(fù)制代碼 代碼如下:

using System;
using System.IO;
using System.Web;
using System.Web.UI;
namespace ZDIL.URLRewriter
{
/**//// <summary>
/// URL重寫(xiě)
/// </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"; //真正要訪問(wèn)的頁(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è)類(lèi)呢,要寫(xiě)在一個(gè)單獨(dú)的項(xiàng)目里面,然后編譯成 ZDILURLRewriter.DLL文件。(注意文件名,寫(xiě)錯(cuò)了就不能正常運(yùn)行了)。
4、完成了。
打開(kāi)IE ,在地址欄里輸入 http://.../1.zhtml。
瀏覽者看到是一個(gè)靜態(tài)頁(yè)的地址,但是實(shí)際上訪問(wèn)的卻是 /web/index.aspx 這個(gè)動(dòng)態(tài)網(wǎng)頁(yè)。
怎么樣簡(jiǎn)單吧。
當(dāng)然了,這個(gè)是最簡(jiǎn)單的,簡(jiǎn)單到了“不能用”的地步了。因?yàn)樗麜?huì)把所有的 *.zhtml 的訪問(wèn)都“重寫(xiě)”到 /web/index.aspx 。
至于把什么樣的網(wǎng)頁(yè)重寫(xiě)到哪個(gè)網(wǎng)頁(yè),這里就不介紹了(這里只講方法,不講實(shí)現(xiàn)的細(xì)節(jié))。
方法很多了,原作是通過(guò)正則來(lái)匹配的,我是通過(guò) string sendToUrl = url; 來(lái)判斷的。
其他的就看你們的需要了。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 偃师市| 黄骅市| 延吉市| 新源县| 临沂市| 潜江市| 宁武县| 铅山县| 师宗县| 凤翔县| 会宁县| 博湖县| 漠河县| 门源| 昌都县| 平塘县| 兴化市| 台南县| 桂阳县| 南投县| 福清市| 姚安县| 万年县| 公安县| 德格县| 仁化县| 义乌市| 新巴尔虎左旗| 什邡市| 米易县| 泰安市| 大宁县| 阳西县| 巴彦淖尔市| 凤台县| 修武县| 柳林县| 呼和浩特市| 北碚区| 潜江市| 桑植县|