URL重写_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > URL重写

URL重写

 2013/7/26 12:18:50  G1mist  博客园  我要评论(0)
  • 摘要:URL重写就是首先获得一个进入的URL请求,然后把它重新写成网站可以处理的另一个URL的过程。举个例子来说,如果通过浏览器进来的URL是"list.1html",那么它可以被重写成"list.aspx?id=1",这样的URL,这样的网址可以更好的被网站所阅读。1.首先新建一个WebApplication项目和一个类库(用于做URLRewrite)2.在index.aspx页面中添加一个按钮用于跳转到另外一个页面(跳转的链接为:list.1html)前台代码:1<
  • 标签:URL

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

    

发表评论
用户名: 匿名