zoukankan      html  css  js  c++  java
  • URL重写

    URL重写

     

      URL重写就是首先获得一个进入的URL请求,然后把它重新写成网站可以处理的另一个URL的过程。举个例子来说,如果通过浏览器进来的URL是"list.aspx?id=1",那么它可以被重写成"list.1html",这样的URL,这样的网址可以更好的被网站所阅读。

      1.首先新建一个WebApplication项目和一个类库(用于做URLRewrite)

      

      2.在index.aspx页面中添加一个按钮用于跳转到另外一个页面(跳转的链接为:list.1html)

      前台代码:

    复制代码
     1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="URLRewriter.index" %>
     2 
     3 <!DOCTYPE html>
     4 
     5 <html xmlns="http://www.w3.org/1999/xhtml">
     6 <head runat="server">
     7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     8     <title></title>
     9 </head>
    10 <body>
    11     <form id="form1" runat="server">
    12         <div>
    13             <asp:Button Text="跳转" runat="server" ID="btnGo" OnClick="btnGo_Click" />
    14         </div>
    15     </form>
    16 </body>
    17 </html>
    复制代码

      后台代码:(跳转到list.1html这个链接)

    1         protected void btnGo_Click(object sender, EventArgs e)
    2         {
    3             Response.Redirect("list.1html");
    4         }

      3.进行URL重写

        Module这个类库中新建一个类:Module.cs,用于URL的重写,首先我们要添加System.Web程序集的引用,之后实现IHttpModule接口

    复制代码
     1     public class Module : IHttpModule
     2     {
     3         public void Dispose()
     4         { }
     5 
     6         /// <summary>
     7         /// 初始化
     8         /// </summary>
     9         /// <param name="context"></param>
    10         public void Init(HttpApplication context)
    11         {
    12             //一个BeginRequest事件
    13             //当浏览器输入URL并跳转的时候,HTTPRequest开始
    14             //在请求开始的时候将会引发这个事件
    15             context.BeginRequest += context_BeginRequest;
    16         }
    17 
    18         /// <summary>
    19         /// BeginRequest事件响应函数
    20         /// </summary>
    21         /// <param name="sender"></param>
    22         /// <param name="e"></param>
    23         void context_BeginRequest(object sender, EventArgs e)
    24         {
    25             //将sender转成HttpApplication对象
    26             HttpApplication app = sender as HttpApplication;
    27 
    28             //获得HTTP请求的路径,在这个例子中将会获得:"/list.1html"这样一个路径
    29             string requestPath = app.Context.Request.Path;
    30 
    31             //我们要获取路径的文件名,这里获得的是 "list.1html"
    32             string fileName = Path.GetFileName(requestPath);
    33 
    34             //判断文件名是否以"list."开头,如果是,我们将进行URL的重写
    35             if (fileName.StartsWith("list."))
    36             {
    37                 //获取文件的拓展名 ".1html"
    38                 string extention = Path.GetExtension(fileName);
    39 
    40                 //进行URL的重写,我们需要得到的是".1html"中的那个"1",而这个"1"的位数是不固定的
    41                 //有可能是"122312321.html"
    42                 //所以我们需要获取"."到"h"之间的那一段数字
    43                 //1.将"html"替换为空字符串,得到的是".1"
    44                 string target = extention.Replace("html", "");
    45                 //2.使用Substring方法取字串,从第一位开始取,一直取到最后一位
    46                 //target的值就为"1"
    47                 target = target.Substring(1);
    48                 //3.获得target值之后就可以重写URL了,在这里我们将"list.1html"重写成"list.aspx?id=1"
    49                 app.Context.RewritePath("list.aspx?id=" + target);
    50             }
    51         }
    复制代码

      4.重写完成之后,页面的请求会跳转到"list.aspx"页面

      后台代码,用来接收和输出传进来的参数

    复制代码
    1  protected void Page_Load(object sender, EventArgs e)
    2         {
    3             if (Request.QueryString["id"] != null)
    4             {
    5                 string id = Request.QueryString["id"].ToString();
    6                 Response.Write(id);
    7             }
    8         }
    复制代码

      5.还有很重要的一步,就是在Web.config中要做相应的配置

    复制代码
     1 <configuration>
     2   <system.web>
     3     <compilation debug="true" targetFramework="4.5" />
     4     <httpRuntime targetFramework="4.5" />
     5   </system.web>
     6   <system.webServer>
     7     <modules>
     8       <add type="Module.Module,Module" name="MyModule" />
     9     </modules>
    10   </system.webServer>
    11 </configuration>
    复制代码

      其中, <add type="Module.Module,Module" name="MyModule" />节点中,name可以随便写,type中逗号前面是URL重写那个类的名称,就是

      "命名空间+类名":Module.Module

      

      逗号后面的是程序集的名称,也就是类库的名称:(Module)

      

      完成这些工作之后,在index.aspx中点击跳转按钮,本应该跳转到"list.1html"页面,而在我们的网站结构中是没有"list.1html"这个页面的,我们进行URL重写了之后,就跳转到了"list.aspx?id=1"

  • 相关阅读:
    uu 模块
    程序员都是好男人
    TCP基础知识
    最全 git 命令总结
    iOS 添加UIWindow不显示问题解决
    解决CFBundleIdentifier", Does Not Exist
    Mac 系统OS X>=10.9,怎么把默认的python切换成3.7或者更高
    OC算法练习-Hash算法
    设计模式架构模式
    runtime相关知识
  • 原文地址:https://www.cnblogs.com/jameslif/p/3227458.html
Copyright © 2011-2022 走看看