各位好,小弟正在学习url重写,在网上看了那些方法好像都不是很全面,而且大多数是一样,不知道各位有做过这方面的没有,发一份我参考一下,谢谢了!

解决方案 »

  1.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Text.RegularExpressions;/// <summary>
    ///URL重写
    /// </summary>
    public class UrlReWriter:IHttpModule
    {
        public UrlReWriter(){}
        public void Dispose()
        {
            throw new NotImplementedException();
            
        }    public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.Error += new EventHandler(context_Error);
        }
        void context_Error(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            context.Response.Write("<html>");
            context.Response.Write("<head><title>出错了!</title></head>");
            context.Response.Write("<body style=\"font-size:14px;\">");
            context.Response.Write("出错了:<br />");
            context.Response.Write("<textarea name=\"errormessage\" style=\"width:80%; height:200px; word-break:break-all\">");
            context.Response.Write(HttpUtility.HtmlEncode(context.Server.GetLastError().ToString()));
            context.Response.Write("</textarea>");
            context.Response.Write("</body></html>");
            context.Response.End();
        }
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            HttpResponse response = context.Response;
            string path = context.Request.Path;
            string file = System.IO.Path.GetFileName(path);        string oldfile = Regex.Replace(file, @"\d+", "");        Regex[] regexs = new Regex[]
            {
                new Regex("djyx(\\d+).aspx", RegexOptions.Compiled),
                new Regex("wlyx(\\d+).aspx", RegexOptions.Compiled),
                new Regex("glmj(\\d+).aspx", RegexOptions.Compiled),
                new Regex("yxzx(\\d+).aspx", RegexOptions.Compiled),
                new Regex("yxpc(\\d+).aspx", RegexOptions.Compiled),
                new Regex("CreativeFun(\\d+).aspx", RegexOptions.Compiled),
                new Regex("News(\\d+).aspx", RegexOptions.Compiled)
            };        foreach (Regex rex in regexs)
            {
                Match match = rex.Match(file);            if (match.Success)
                {
                    string userId = match.Groups[1].Value;
                    string rewritePath = oldfile+ "?index=" + userId;
                    context.RewritePath(rewritePath);                break;
                }
           }
        }}
      

  2.   

    url重写有很多方法,只要用一个就行了
      

  3.   

    http://www.blueidea.com/tech/program/2009/6551.asp
    关键就是将URL截取 然后跳转
      

  4.   

    web.config 
     下面的 重写 可能好点把