zoukankan      html  css  js  c++  java
  • URLWrite

    添加一个global.asax文件新增如下代码

      protected void Application_BeginRequest(object sender, EventArgs e)
        {
            string oldUrl = HttpContext.Current.Request.RawUrl;
            UrlRewrite(oldUrl, @"^(.*)/Index\.htm", @"Default1.aspx");
            UrlRewrite(oldUrl, @"^(.+)/view-(\d+)-(.+)\.htm(\?.*)*$", "$1/Default2.aspx?id=$2&type=$3");
            UrlRewrite(oldUrl, @"^(.+)/toptic-(.+)\.htm(\?.*)*$", "$1/view/viewDetail.aspx?ID=$2");

            //(@"^(.+)/(\d+)\.htm(\?.*)*$", "$1/Flue/ListView.aspx?id=$2")  任意字符/数字.htm 对应 flue/listview.aspx?id=数字
        }
        private void UrlRewrite(string oldUrl, string pattern, string replace)
        {
            if (Regex.IsMatch(oldUrl, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled))
            {
                string newUrl = Regex.Replace(oldUrl, pattern, replace, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                this.Context.RewritePath(newUrl);
            }
        }

    、、、、、、、、、、、、、、、、、、、、、、、

    另外,微软还提供了一个dll文件,URLWriter.dll ,

    我们也可以使用它来直接进行URL重写。

    ①添加引用URLwriter.dll文件

    ②在web.config文件中,找到<configSections>节点,在结束标志</configSections>前添加代码

    第一步
    <configSections>
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandlers, URLRewriter">

    </configSections>

    ③在web.config文件中,还是找到<configSections>节点,在结束标志</configSections>后,添加代码

    第二步
    <RewriterConfig>
    <Rules>
    <RewriterRule>
    <LookFor>~/user/blogs.aspx</LookFor>
    <SendTo>~/user/blogs.aspx?id=$1</SendTo>
    </RewriterRule>
    </Rules>
    </RewriterConfig>

    ④找到节点<httpHandlers>,在<httpHandlers>中添加

    <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler,URLRewriter"/>
  • 相关阅读:
    学习方法
    仿知乎Android端回答UI
    【LeetCode】:二叉树的Max,Min深度
    LeetCode:二叉树的前、中、后序遍历
    Caffe学习系列(四)之--训练自己的模型
    后端开发--之文件上传
    Python——轻量级web服务器flask的学习
    Django 部署(Apache下)
    Caffe学习系列(三)Docker安装及一些问题的记录
    Caffe学习系列(二)Caffe代码结构梳理,及相关知识点归纳
  • 原文地址:https://www.cnblogs.com/ajun/p/2239040.html
Copyright © 2011-2022 走看看