求一正则匹配表达式

啾啾我 2014-05-10 11:12:22
        
pattern = string.Format(@"/(.[0-9a-zA-Z]*)/$");
if (Regex.IsMatch(requestPath, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled))
{
string file = requestPath.Substring(0, requestPath.Length - 1) + ".aspx"; ;
file = Server.MapPath("~" + file);
if (System.IO.File.Exists(file))
file = "/$1.aspx";
else
file = "/default.aspx?param=$1";
string newUrl = Regex.Replace(requestPath, pattern, file, RegexOptions.IgnoreCase | RegexOptions.Compiled);
Context.RewritePath(newUrl);
}

我上传到服务,默认文档设置为default.aspx
当我http://abc.cn的时候访问正常
当我http://abc.cn/news/的时候访问的还是网站首页,应该是news文件夹下的default.aspx文件才对
当我http://abc.cn/b/的时候访问访问的还是网站首页,应该访问的是b文件夹下的default.aspx文件才对
当我http://abc.cn/aboutUs/notice/的时候访问的http://abc.cn/aboutUs/default.aspx,应该访问的是notice文件夹下的default.aspx文件才对

求各位写一个正确的正则,在线等!!!
测试没有问题,在追加40分,急………………
...全文
183 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2014-05-11
  • 打赏
  • 举报
回复

        pattern = string.Format(@"/(.[0-9a-zA-Z]*)/$");
        if (Regex.IsMatch(requestPath, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled))
        {
            string file = requestPath.Substring(0, requestPath.Length - 1) + ".aspx"; ;
            file = Server.MapPath("~" + file);          
            if (System.IO.File.Exists(file))
                file = "/$1.aspx";
            else
                file = "/default.aspx?param=$1";
               // 这里的问题, /default.aspx?param=$1  代表的是根目录,需要把路径加到这里,用这则提取一下路径加进来就行 了
            string newUrl = Regex.Replace(requestPath, pattern, file, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            Context.RewritePath(newUrl);
        }
啾啾我 2014-05-10
  • 打赏
  • 举报
回复
@"(?<=[^\.cn]/)([0-9a-zA-Z|]*)/$" 我这样去写, 当我http://abc.cn/aboutUs/notice/的时候访问的http://abc.cn/aboutUs/default.aspx,应该访问的是notice文件夹下的default.aspx文件才对,其他访问都正常
啾啾我 2014-05-10
  • 打赏
  • 举报
回复
引用 1 楼 Return_false 的回复:
正则修改如下 试试 @"/([^/]*?)/$"
还是不行, 假如我输入http://abc.cn/news/ 匹配结果是:http://abc.cn/default.aspx?param=news 或者http://abc.cn/ 匹配结果是http://default.aspx?param=abc.cn
  • 打赏
  • 举报
回复
正则修改如下 试试 @"/([^/]*?)/$"

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧