• ASP.NET之:URL重写(转载)


    public class UrlRouteModule : IHttpModule
        {
            private static string URL_FLAG = "/q/"; //Url中区别路径和参数的分隔符
            private static string URL_SUFFIX = ".aspx"; //对哪种后缀的Url实施Rewrite
    
            public void Init(HttpApplication context)
            {
                context.BeginRequest += new EventHandler(context_BeginRequest);
            }
    
            private void context_BeginRequest(object sender, EventArgs e)
            {
                HttpApplication app = sender as HttpApplication;
                if (app == null) return;
    
                string currentUrl = app.Context.Request.RawUrl;
                if (currentUrl.EndsWith(URL_SUFFIX, StringComparison.OrdinalIgnoreCase) == false) //后缀不符合的跳过
                    return;
    
                int p = currentUrl.IndexOf(URL_FLAG, StringComparison.OrdinalIgnoreCase); //无参的也跳过
                if (p == -1) return;
    
                currentUrl = currentUrl.Substring(0, currentUrl.Length - URL_SUFFIX.Length); //去除后缀
                string url = string.Format("{0}.aspx", currentUrl.Substring(0, p));
                string query = FormmatUrlToQuery(currentUrl.Substring(p + URL_FLAG.Length));
    
                app.Context.RewritePath(url, string.Empty, query);
            }
    
            private string FormmatUrlToQuery(string url)
            {
                int j = 0; //计数器
                int len = url.Length;
                char[] chars = new char[len];
    
                for (int i = 0; i < len; i++)
                {
                    if (url[i] != '/')
                        chars[i] = url[i];
                    else
                    {
                        if (++j % 2 == 1)
                            chars[i] = '=';
                        else
                            chars[i] = '&';
                    }
                }
    
                return new string(chars);
            }
    
            public void Dispose() { }
        }
    
  • 相关阅读:
    心跳机制
    C++虚继承和虚基类
    STL,ATL与WTL
    C# Task的暂停与终止
    C#继承
    C#线程同步问题
    CourtAi发布配置文件修改说明
    阿里云虚拟主机https化步骤第一篇,申请证书(笔记)
    linux 服务器重启指令
    .net core 发布到iis问题 HTTP Error 500.30
  • 原文地址:https://www.cnblogs.com/zjwei55/p/2146308.html
Copyright © 2020-2023  润新知