asp.net 内部重定向

1.

复制代码
        /*
         * 2、
         * Context.RewritePath()
         * 使用给定路径重写 URL。(内部重写)
         * 内部请求重写
         */
        public static void TestTwo()
        {
            //带有参数 处理
            //http://localhost:49796/testtwo/{id}
            string result = "";
            if (Contains(AbsolutePath, @"testtwo/\d+$", out result))
            {
                RewritePath(GetVirtualPath("~/test/testone.aspx?id=" + GetIntStr(result)));
            }

            //没有参数
            //http://localhost:49796/testtwo{*catchall}
            if (AbsolutePath.Contains("testtwo"))
            {
                RewritePath(GetVirtualPath("~/test/testone.aspx"));
            }
        }
        /*
         * 3、
         * Context.RewritePath()
         * 使用给定路径重写 URL。(内部重写)
         * 内部请求执行转移
         */
        public static void TestThree()
        {
            //带有参数 处理
            //http://localhost:49796/testtwo/{id}/{name}/
            string result = "";
            if (Contains(AbsolutePath, @"testthree/\d+/\w+/$", out result))
            {
                result = result.Replace("testthree","");
                _Server.Transfer("~/test/testone.aspx?id=" + GetIntStr(result) + "&name=" + GetStringStr(result));
            }

            //没有参数
            if (AbsolutePath.Contains("testthree"))
            {
                _Server.Transfer("~/test/testone.aspx");
            }
        }
复制代码

正则匹配

复制代码
        //判断 制定的字符串 在源字符串中是否匹配
        //返回 第一个匹配项
        private static bool Contains(string source, string target, out string firstMatch)
        {
            firstMatch = "";

            Match result = Regex.Match(source, target, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            if (result.Success)
            {
                firstMatch = result.Value;
                return true;
            }
            return false;
        }

        //获取字符串中的第一个数字字符匹配项
        private static string GetIntStr(string source)
        {
            Match result = Regex.Match(source, @"(\d+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            if (result.Success)
            {
                return result.Value;
            }
            return "";
        }
        //获取字符串中 第一个字符串匹配项
        private static string GetStringStr(string source)
        {
            Match result = Regex.Match(source, @"([a-z_]+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            if (result.Success)
            {
                return result.Value;
            }
            return "";
        }
复制代码

2.两张图片对比区别

 

posted @   天马3798  阅读(333)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示