µ±Ç°Î»ÖãºÊ×Ò³ > ÍøÂç±à³Ì > WEB±à³Ì > ASP.net > asp.net URLÖØÐ´£¨URLRewriter£© Ö®¼ò»¯°æ

asp.net URLÖØÐ´£¨URLRewriter£© Ö®¼ò»¯°æ

µã»÷´ÎÊý£º29 ´Î ·¢²¼ÈÕÆÚ£º2008-11-22 11:30:49 ×÷ÕߣºÔ´´úÂëÍø
Ô´´úÂëÍøÍÆ¼ö

ÔÚ asp.net ÀïʵÏÖ URLÖØÐ´£¨URLRewriter£©µÄÒ»¸ö×î¼òµ¥µÄ·½·¨¡£

¡¡¡¡²Î¿¼ÁË £¨×÷Õß Scott Mitchell ·­Ò룺Janssen £©µÄ´ó×÷£¬ËäȻûÓÐÍêÈ«¿´Ã÷°×£¬µ«ÊÇÒ²ÕÕè»­»¢µØ×öÁËÒ»¸ö£¬ÆÄÓС°³É¾Í¡±¸Ð¡£Ð´³öÀ´·ÖÏíһϡ£

¡¡¡¡Ô­×÷Àï½²Á˺ܶàµÄÔ­Àí£¬ÕâÀï¾Í²»ËµÁË£¨ÆäʵÎÒÒ²²»¶®£©¡£ÕâÀï¾Íд²Ù×÷¹ý³Ì°É¡£Ä¿µÄÊÇʵÏÖÒ»¸ö×î¼òµ¥µÄÄÜʵÏÖ URLÖØÐ´ µÄ³ÌÐò¡£

¡¡¡¡1¡¢ÐèÒªÉèÖÃÒ»ÏÂIISÀïµÄÕ¾µãÊôÐÔ¡£

¡¡¡¡2¡¢ÐÞ¸Äweb.configµÄÄÚÈÝ¡£

<system.web>
¡¡¡¡¡¡<httpHandlers>
¡¡¡¡¡¡¡¡<add verb="*" path="*.zhtml" type="ZDIL.URLRewriter.RewriterFactoryHandler, ZDILURLRewriter" />
¡¡¡¡</httpHandlers>
</system.web>

¡¡¡¡ÆäÖÐ*.zhtml ¾ÍÊǵØÖ·À¸ÀïÃæÐ´µÄÍøÒ³µÄÀ©Õ¹Ãû£¬¾ÍÊǸøÓû§¿´µÄ£¬Õâ¸ö¿ÉÒÔËæÒâ¸Ä£¨µ«ÊÇÒª·ûºÏÀ©Õ¹ÃûµÄ¹æÔò£¡£©¡£µ±È»ÒªºÍµÚÒ»²½ÀïÃæµÄÉèÖÃÏàÒ»Ö²ÅÐС£

¡¡¡¡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 = "";¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ //²ÎÊý¡£±ÈÈç ?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Îļþ¡££¨×¢ÒâÎļþÃû£¬Ð´´íÁ˾Ͳ»ÄÜÕý³£ÔËÐÐÁË£©¡£

¡¡¡¡4¡¢Íê³ÉÁË¡£

¡¡¡¡´ò¿ªIE £¬ÔÚµØÖ·À¸ÀïÊäÈë http://.../1.zhtml¡£

¡¡¡¡ä¯ÀÀÕß¿´µ½ÊÇÒ»¸ö¾²Ì¬Ò³µÄµØÖ·£¬µ«ÊÇʵ¼ÊÉÏ·ÃÎʵÄÈ´ÊÇ /web/index.aspx Õâ¸ö¶¯Ì¬ÍøÒ³¡£

¡¡¡¡ÔõôÑù¼òµ¥°É¡£

¡¡¡¡µ±È»ÁË£¬Õâ¸öÊÇ×î¼òµ¥µÄ£¬¼òµ¥µ½ÁË¡°²»ÄÜÓᱵĵز½ÁË¡£ÒòΪËû»á°ÑËùÓÐµÄ *.zhtml µÄ·ÃÎʶ¼¡°ÖØÐ´¡±µ½ /web/index.aspx ¡£

¡¡¡¡ÖÁÓÚ°ÑʲôÑùµÄÍøÒ³ÖØÐ´µ½ÄĸöÍøÒ³£¬ÕâÀï¾Í²»½éÉÜÁË£¨ÕâÀïÖ»½²·½·¨£¬²»½²ÊµÏÖµÄϸ½Ú£©¡£

¡¡¡¡·½·¨ºÜ¶àÁË£¬Ô­×÷ÊÇͨ¹ýÕýÔòÀ´Æ¥ÅäµÄ£¬ÎÒÊÇͨ¹ý string sendToUrl = url; À´Åжϵġ£

¡¡¡¡ÆäËûµÄ¾Í¿´ÄãÃǵÄÐèÒªÁË¡£

¡¡¡¡http://blog.csdn.net/shixin1198/archive/2006/10/16/1336846.aspx

Ô´´úÂëÍø¹©¸å.
ÍøÓÑÆÀÂÛ (0)
»áÔ±ÖÐÐÄ
ÍøÂç±à³Ì
±¾Õ¾ÍƼö
ÍøÂç±à³ÌÖ®¾«»ª