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

首頁 > 編程 > .NET > 正文

asp.net URL重寫簡化版 速學URL重寫

2024-07-10 12:42:43
字體:
來源:轉載
供稿:網友
在 asp.net 里實現(xiàn) URL重寫(URLRewriter)的一個最簡單的方法。
參考了 (作者 Scott Mitchell 翻譯:Janssen )的大作,雖然沒有完全看明白,但是也照貓畫虎地做了一個,頗有“成就”感。寫出來分享一下。
原作里講了很多的原理,這里就不說了(其實我也不懂)。這里就寫操作過程吧。目的是實現(xiàn)一個最簡單的能實現(xiàn) URL重寫 的程序。
1、需要設置一下IIS里的站點屬性。

2、修改web.config的內容。

代碼如下:
<system.web>
<httpHandlers>
<add verb="*" path="*.zhtml" type="ZDIL.URLRewriter.RewriterFactoryHandler, ZDILURLRewriter" />
</httpHandlers>
</system.web>


其中*.zhtml 就是地址欄里面寫的網頁的擴展名,就是給用戶看的,這個可以隨意改(但是要符合擴展名的規(guī)則!)。當然要和第一步里面的設置相一致才行。
3、寫一個類。

代碼
代碼如下:
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"; //真正要訪問的頁面
string queryString = ""; //參數(shù)。比如 ?id=123
filePath = context.Server.MapPath(sendToURLString); //物理地址
//這句最重要了。轉向了。
context.RewritePath(sendToURLString, String.Empty, queryString);
return PageParser.GetCompiledPageInstance(url, filePath, context);
}
public virtual void ReleaseHandler(IHttpHandler handler)
{
}
}
}


這個類呢,要寫在一個單獨的項目里面,然后編譯成 ZDILURLRewriter.DLL文件。(注意文件名,寫錯了就不能正常運行了)。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 青冈县| 抚州市| 盐源县| 沧源| 竹北市| 府谷县| 达日县| 府谷县| 永城市| 临江市| 射阳县| 班玛县| 曲沃县| 台东县| 分宜县| 永仁县| 延长县| 涞水县| 任丘市| 拜泉县| 广平县| 商都县| 象州县| 永城市| 靖安县| 定州市| 商水县| 秀山| 达孜县| 肇州县| 潞城市| 临朐县| 鹤庆县| 北川| 故城县| 渝北区| 湖南省| 石阡县| 沾化县| 宁晋县| 翁源县|