H.Wong

P.

导航

2008年我来了...呵呵,我也跟风.

这几天在公司闲着,无聊,于是研究了一下URL重写.不过没有写完代码.贴出来权当是保存.

一下是ReUrlModule.HttpModule的所有类.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ReUrlModule.HttpModule
{
    sealed class URLrewrite
    {
        static string[] strImgs
        {
            get
            {
                return new string[] { ".jpg",".gif",".bmp",".tif"};
            }
        }
        public static  string strRewrite(string strRequestURL)
        {
            int I = 0;
            for (int i = 0; i < strImgs.Length; i++)
            {
                if (strRequestURL.IndexOf(strImgs[i]) > 0)
                {
                    I += 1;
                }
            }
            if (I > 0)
            {
                return strRequestURL;
            }
            else
            {
                return strQuerys(strRequestURL)[1].Replace("/","") +"|"+ strQuerys(strRequestURL)[0];
            }
        }

        public static string strRewriteContext(string strRequestURL)
        {
            int I = 0;
            for (int i = 0; i < strImgs.Length; i++)
            {
                if (strRequestURL.IndexOf(strImgs[i]) > 0)
                {
                    I += 1;
                }
            }
            if (I > 0)
            {
                return strRequestURL;
            }
            else
            {
                return strQuerys(strRequestURL)[1] + "?" + strQuerys(strRequestURL)[0];
            }
        }


        static string strRepUrl(string strURL)
        {
            string str = strURL;
            string[] tmpstring = new string[3] { "http",":",@"//"};

            for (int i = 0; i < tmpstring.Length; i++)
            {
                str = str.ToLower().Replace(tmpstring[i], "");
            }
            int iEnd = str.IndexOf("/");
            str = str.Replace(str.Substring(0,iEnd),"");
            return str;
        }
        static string strQuery(string strUrl)
        {
            //strUrl = strRepUrl(strUrl);
            if (strUrl.IndexOf("?") > 0)
            {
                int iStart = strUrl.IndexOf("?") + 1;
                int iLength = strUrl.Length;
                string tmp = strUrl.Substring(iStart, iLength - iStart);
                return tmp;
            }
            else
            {
                return " ";
            }
        }

        static string[] strQuerys(string strURL)
        {
            string tmpQuery = strQuery(strURL);
            string tmpURL = strRepUrl(strURL).Replace(tmpQuery,"").Replace("?","");
            int iLast = tmpURL.LastIndexOf("/")+1;
            tmpURL = tmpURL.Substring(0, iLast);
            string[] tmp = new string[2] {tmpQuery,tmpURL };
            return tmp;
        }
    }
}

----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace ReUrlModule.HttpModule
{
    sealed class URLmodule:IHttpModule
    {

        #region IHttpModule 成员

        public void Dispose()
        {
           
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.EndRequest += new EventHandler(context_EndRequest);
            context.Error += new EventHandler(context_Error);
            context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
           
           
        }

        void context_AcquireRequestState(object sender, EventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
            HttpApplication oHttpApp = (HttpApplication)sender;
            HttpServerUtility oServer = oHttpApp.Server;
            HttpRequest oHttpRequest = oHttpApp.Request;
            HttpResponse oHttpResponse = oHttpApp.Response;
            HttpContext oHttpcontext = oHttpApp.Context;
           
            string strURL = URLrewrite.strRewrite(oHttpRequest.Url.AbsoluteUri);

            oServer.Transfer("/Default.aspx?ID="+strURL);
            //oHttpResponse.Write(strURL);
        }

        void context_AuthorizeRequest(object sender, EventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
           
        }

        void context_Error(object sender, EventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void context_EndRequest(object sender, EventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
           
        }

        #endregion
    }
}

--------------------------------------------------------------------------------------------

using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace ReUrlModule.HttpModule
{
    class FormApdater:System.Web.UI.Adapters.ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {

            if (writer is Html32TextWriter)
            {
                writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
            }
            else
            {
                writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
            }
            base.Render(writer);
        }
    }
}

---------------------------------------------------------------------------------------------

using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace ReUrlModule.HttpModule
{
    public class FormAction:Page
    {
        protected string strURL
        {
            get { return URLrewrite.strRewrite(HttpContext.Current.Request.Url.AbsolutePath); }
        }
        public FormAction()
        {
           
        }

        /// <summary>
        /// get form action contents
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
           
            if (writer is Html32TextWriter)
            {
                writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
            }
            else
            {
                writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
            }
            base.Render(writer);
        }

    }
    internal class FormFixerHtml32TextWriter : Html32TextWriter
    {
        private string _strURL;
        internal FormFixerHtml32TextWriter(TextWriter txtWriter)
            : base(txtWriter)
        {
            _strURL = URLrewrite.strRewriteContext(HttpContext.Current.Request.Url.AbsoluteUri);
        }
        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if (_strURL != null && string.Compare(name, "action", true) == 0)
            {
                value = _strURL;
            }
            base.WriteAttribute(name, value, fEncode);
        }
    }

    internal class FormFixerHtmlTextWriter : HtmlTextWriter
    {
        private string _strURL = string.Empty;
        internal FormFixerHtmlTextWriter(TextWriter txtWriter)
            : base(txtWriter)
        {
            _strURL = URLrewrite.strRewriteContext(HttpContext.Current.Request.Url.AbsoluteUri);
        }

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if (_strURL != null && string.Compare(name, "action", true) == 0)
            {
                value = _strURL;
            }
            base.WriteAttribute(name, value, fEncode);
            base.WriteAttribute(name, value, fEncode);
        }

    }
}

--------------------------------------------------------------------------------------------
一下是前台基类的PageBase:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace ReUrlModule.WebApp
{
    public class PageBase:ReUrlModule.HttpModule.FormAction
    {

    }
}

---------------------------------------------------------------------------------------------
以下是网站的web.config文件的配置:

<?xml version="1.0"?>

<configuration>
 
    <appSettings/>
    <connectionStrings/>
 
    <system.web>
  <pages pageBaseType ="ReUrlModule.HttpModule.FormAction,ReUrlModule.HttpModule" />
        <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
        <compilation debug="true" />
        <!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
        -->
        <authentication mode="Windows" />
        <!--
            The <customErrors> section enables configuration
            of what to do if/when an unhandled error occurs
            during the execution of a request. Specifically,
            it enables developers to configure html error pages
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
  <httpModules>
   <add name="RewriteURL" type="ReUrlModule.HttpModule.URLmodule,ReUrlModule.HttpModule" />
  </httpModules>
    </system.web>
</configuration>

----------------------------------------------------------------------------------------------
以上的代码没有注释,但是也不需要都是从网上收罗的.也就是不是自己的东东.只是自己总结了一下,上面的代码没有写全.
还需要处理Css,图片的地址问题,不过假如是用二级站点架构或者是使用CDN网络的话基本上就不需要了.
好像还有个SSO的类库.我不知道放那里了,找到了就发出来吧
趁现在还有心情写点东西呵呵.

posted on 2007-12-31 17:43  H.Wong  阅读(450)  评论(0编辑  收藏  举报