zoukankan      html  css  js  c++  java
  • asp.net Url重写

    1.新建MyModule.cs继承IHttpModule,实现BeginRequest事件。

     public class MyModule:IHttpModule
        {
            public void Init(HttpApplication context)
            {
                context.BeginRequest += CheckPage;
            }
    
            private void CheckPage(object sender, EventArgs e)
            {
                HttpApplication app  = sender as HttpApplication;
                string urlStr = app.Request.RawUrl;  //获取原始地址url  Good_1.html
                Regex regex = new Regex(@"w+_+d+.html");  //正则匹配,英文+下划线+数字+.html类型
                if (regex.IsMatch(urlStr))
                {
                    int line = urlStr.IndexOf('_');  
                    int dot = urlStr.IndexOf('.');
    
                    string id = urlStr.Substring(line + 1, dot - line - 1);  //找出页面跳转的Id
                    string directUrl = "~/Good_list.aspx?id=" + id;       //拼接成动态网页,加上Id
                    app.Server.Transfer(directUrl);                //重定向新的网址
                }
            }
    
            public void Dispose()
            {
                throw new NotImplementedException();
            }
        }

    2.在Web.config配置中注册httpModule

    <system.webServer>
            <modules>
                <add name="myModule" type="WebApplication7.MyModule"/>
            </modules>
    </system.webServer>

    3.建立Good_list.aspx页面,在cs方法内调用

     public partial class Good_list : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string id = Context.Request["id"];
                    Context.Response.Write("这是" + id + "商品");
                }
            }
        }

    本文来自博客园,作者:云辰,转载请注明原文链接:https://www.cnblogs.com/yunchen/p/14684128.html

  • 相关阅读:
    Linux搭建maven私服
    eclipse提交项目到GitHub
    eclipse安装sts插件
    idea配置jdk
    C语言之链表————(转载)
    链表(创建,插入,删除和打印输出(转载)
    C语言之链表
    ARM 之LCD和LCD控制器
    ARM的两种启动方式 (NAND FLASH. NOR FLASH)
    ARM 汇编器对C的扩展
  • 原文地址:https://www.cnblogs.com/yunchen/p/14684128.html
Copyright © 2011-2022 走看看