.NET重写URL方法谈

对于.NET重写URL,也就是Rewriter URL相信大家都不太陌生。在这里我们要介绍的也是.NET重写URL方法,希望能为大家打开新的思路。

最近小项目要求重写url找了下资料用到了MS的2个dll,微软的例子写得太不明显了。后来终于改好了。

ActionlessForm.dll------用来处理回发

URLRewriter.dll----- 是微软封装好了的一个URL重写组件

添加引用----

具体的使用说明请去看

http://msdn.microsoft.com/zh-cn/library/ms972974.aspx#XSLTsection123121120120

比我说得好得多。。

具体使用方法:

首先web.config的配置:

 
 
 
 
  1. <?xml version="1.0"?> 
  2. <configuration> 
  3.   <configSections> 
  4.     <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler,  
  5.  URLRewriter" /> 
  6.   </configSections> 
  7.     <RewriterConfig> 
  8.         <Rules> 
  9.             <RewriterRule> 
  10.                 <LookFor>~/ListCategories\.aspx</LookFor> 
  11.                 <SendTo>~/Default.aspx</SendTo> 
  12.             </RewriterRule> 
  13.             <RewriterRule> 
  14.                 <LookFor>~/(\d+)\.html</LookFor> 
  15.                 <SendTo>~/Cover.aspx?id=$1</SendTo> 
  16.             </RewriterRule> 
  17.         </Rules> 
  18.     </RewriterConfig> 
  19.     <system.web> 
  20.         <httpModules> 
  21.             <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/> 
  22.         </httpModules> 
  23.         <compilation debug="true"/> 
  24. </system.web> 
  25. </configuration> 

主要配置的代码是这些。其他的根据自己的需要和.net的版本自行添加。

然后Default.aspx,Cover.aspx,新建2个页面

Default.aspx:

 
 
 
 
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml"> 
  6. <head runat="server"> 
  7.     <title>无标题页</title> 
  8. </head> 
  9. <body> 
  10.     <form id="form1" runat="server"> 
  11.     <div> 
  12.     <a href="ListCategories.aspx">ListCategories.aspx</a> 
  13.     <a href="30.html">30.html</a> 
  14.     </div> 
  15.     </form> 
  16. </body> 
  17. </html> 

Cover.aspx:

 
 
 
 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cover.aspx.cs" Inherits="Cover" %> 
  2. <%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %> 
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml"> 
  7. <head runat="server"> 
  8.     <title>Cover</title> 
  9. </head> 
  10. <body> 
  11.     <skm:form id="form1" runat="server"> 
  12.     <div> 
  13.     Cover页面  
  14.     <h4><a href="javascript:void(0)" onclick="history.go(-1)">返回上一页</a></h4> 
  15.         <asp:Button ID="Button1" runat="server" Text="Button" /> 
  16.     </div> 
  17.     </skm:form> 
  18. </body> 
  19. </html> 

Cover.aspx.cs:

 
 
 
 
  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13.  
  14. public partial class Cover : System.Web.UI.Page  
  15. {  
  16.     protected void Page_Load(object sender, EventArgs e)  
  17.     {  
  18.         if (Request.QueryString["id"] == null)  
  19.         {  
  20.             Response.End();  
  21.         }  
  22.         else  
  23.         {  
  24.             int id = Convert.ToInt32(Request.QueryString["id"]);  
  25.             Response.Write(id);  
  26.         }  
  27.     }  

还要去对IIS设置:

这样的话伪静态就可以用了

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll ---这是上面的路径

浏览Default.aspx页

ListCategories.aspx --页面其实在服务器上面是没有的。它里面的内容是Default.aspx的内容,因为配置文件里面设置了

写得有点乱。

本例子是和微软的重写url基本一样的。算是简单化了一点点呵呵。

原文标题:.net重写url浅谈

链接:http://www.cnblogs.com/ret00100/archive/2009/10/12/1581778.html

【编辑推荐】

  1. 详解ASP.NET MVC分页的实现方法
  2. ASP.NET MVC与WebForm区别谈
  3. ASP.NET MVC应用程序执行过程分析
  4. ASP.NET MVC分页控件的实现
  5. 有关ASP.NET MVC框架的一些基础知识
THE END