URL重写
2013-07-26 11:20
176 查看
URL重写就是首先获得一个进入的URL请求,然后把它重新写成网站可以处理的另一个URL的过程。举个例子来说,如果通过浏览器进来的URL是"list.aspx?id=1",那么它可以被重写成"list.1html",这样的URL,这样的网址可以更好的被网站所阅读。
1.首先新建一个WebApplication项目和一个类库(用于做URLRewrite)
2.在index.aspx页面中添加一个按钮用于跳转到另外一个页面(跳转的链接为:list.1html)
前台代码:
后台代码:(跳转到list.1html这个链接)
3.进行URL重写
Module这个类库中新建一个类:Module.cs,用于URL的重写,首先我们要添加System.Web程序集的引用,之后实现IHttpModule接口
4.重写完成之后,页面的请求会跳转到"list.aspx"页面
后台代码,用来接收和输出传进来的参数
5.还有很重要的一步,就是在Web.config中要做相应的配置
其中, <add type="Module.Module,Module" name="MyModule" />节点中,name可以随便写,type中逗号前面是URL重写那个类的名称,就是
"命名空间+类名":Module.Module
逗号后面的是程序集的名称,也就是类库的名称:(Module)
完成这些工作之后,在index.aspx中点击跳转按钮,本应该跳转到"list.1html"页面,而在我们的网站结构中是没有"list.1html"这个页面的,我们进行URL重写了之后,就跳转到了"list.aspx?id=1"
1.首先新建一个WebApplication项目和一个类库(用于做URLRewrite)
2.在index.aspx页面中添加一个按钮用于跳转到另外一个页面(跳转的链接为:list.1html)
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="URLRewriter.index" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button Text="跳转" runat="server" ID="btnGo" OnClick="btnGo_Click" /> </div> </form> </body> </html>
后台代码:(跳转到list.1html这个链接)
protected void btnGo_Click(object sender, EventArgs e) { Response.Redirect("list.1html"); }
3.进行URL重写
Module这个类库中新建一个类:Module.cs,用于URL的重写,首先我们要添加System.Web程序集的引用,之后实现IHttpModule接口
public class Module : IHttpModule { public void Dispose() { } /// <summary> /// 初始化 /// </summary> /// <param name="context"></param> public void Init(HttpApplication context) { //一个BeginRequest事件 //当浏览器输入URL并跳转的时候,HTTPRequest开始 //在请求开始的时候将会引发这个事件 context.BeginRequest += context_BeginRequest; } /// <summary> /// BeginRequest事件响应函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void context_BeginRequest(object sender, EventArgs e) { //将sender转成HttpApplication对象 HttpApplication app = sender as HttpApplication; //获得HTTP请求的路径,在这个例子中将会获得:"/list.1html"这样一个路径 string requestPath = app.Context.Request.Path; //我们要获取路径的文件名,这里获得的是 "list.1html" string fileName = Path.GetFileName(requestPath); //判断文件名是否以"list."开头,如果是,我们将进行URL的重写 if (fileName.StartsWith("list.")) { //获取文件的拓展名 ".1html" string extention = Path.GetExtension(fileName); //进行URL的重写,我们需要得到的是".1html"中的那个"1",而这个"1"的位数是不固定的 //有可能是"122312321.html" //所以我们需要获取"."到"h"之间的那一段数字 //1.将"html"替换为空字符串,得到的是".1" string target = extention.Replace("html", ""); //2.使用Substring方法取字串,从第一位开始取,一直取到最后一位 //target的值就为"1" target = target.Substring(1); //3.获得target值之后就可以重写URL了,在这里我们将"list.1html"重写成"list.aspx?id=1" app.Context.RewritePath("list.aspx?id=" + target); } }
4.重写完成之后,页面的请求会跳转到"list.aspx"页面
后台代码,用来接收和输出传进来的参数
protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["id"] != null) { string id = Request.QueryString["id"].ToString(); Response.Write(id); } }
5.还有很重要的一步,就是在Web.config中要做相应的配置
<configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <system.webServer> <modules> <add type="Module.Module,Module" name="MyModule" /> </modules> </system.webServer> </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"
相关文章推荐
- 在ASP.NET中重写URL
- 好多模板网分析之URL重写
- 在 ASP.NET 中执行 URL 重写
- apache重写带有get参数的url地址
- 在 ASP.NET 中执行 URL 重写
- ZENCART Ultimate SEO URLs重写规则产品页URL
- 在 ASP.NET 中执行 URL 重写(ZZ FROM MSDN)
- URL重写
- kohana环境配置与URL重写
- Nginx实战(二) URL重写
- URL重写的几种实现方式的总结
- Url重写之action改变的问题
- url重写后页面回传问题
- URL 重写(转载)
- ASP.NET 中执行URL 重写
- 使用windows上的 MOD_REWRITE 启用 url 重写
- Url 重写
- .net4.0(VS2010平台) url重写
- 博客园URL重写月份那里 存储过程代码应该是这样的
- 在 ASP.NET 中执行 URL 重写[网淘]