(原创)无需修改IIS在.NET2005中实现无扩展名的URL重写

来源:互联网 发布:xy传奇盛世翅膀数据 编辑:程序博客网 时间:2024/05/15 23:52

 此篇文章感谢福娃儿,在他的文章基础上加以改进

当在程序中引入了ActionlessForm.dll,UrlRewriter.dll两个类库文件后

在Web.Config配置文件中,添加如下代码

 

<?xml version="1.0"?>
<configuration>
  
    
<configSections>
        
<section name="CustomConfiguration" type="URLRewriter.Config.UrlsSection, URLRewriter"/>
    
</configSections>
  
    
<CustomConfiguration>
        
<urls>
            
<add virtualUrl="~/products/(.+).aspx" destinationUrl="~/products.aspx?ID=$1"/>
           
<add virtualUrl="~/(.+)/(.+)/(.+)/(.+).aspx" destinationUrl="~/products.aspx?ID=$1,$2,$3,$4"/>
           
<add virtualUrl="~/(.+)" destinationUrl="~/products.aspx?ID=$1"/>
        
</urls>
    
</CustomConfiguration>
  
    
<system.web>
        
<httpModules>
            
<add type="URLRewriter.RewriterModule, URLRewriter" name="RewriterModule"/>
        
</httpModules>
    
        
<authentication mode="Forms"/>
    
        
<compilation debug="true"/>
    
  
</system.web>
</configuration>

其中的<add virtualUrl="~/(.+)" destinationUrl="~/products.aspx?ID=$1"/>标记解释为

如果请求的虚地址www.abc.com/任何字符,均被定向到www.abc.com/products.aspx?ID=任何字符

这样,就可以实现BLOG中的blog.abc.com/fushuai效果了

OK,建立个Products.aspx,Default.aspx页面

在Default上写个链接

<asp:HyperLink ID="HyperLink9" runat="server" NavigateUrl="~/2008/2/21/Car.aspx">2008/2/21/Car.aspx</asp:HyperLink><br />
        
<asp:HyperLink ID="HyperLink10" runat="server" NavigateUrl="~/products/Car.aspx">products/Car.aspx</asp:HyperLink><br />
        
<asp:HyperLink ID="HyperLink11" runat="server" NavigateUrl="~/fushuai">无扩展名fushuai</asp:HyperLink><br />

然后在Products的PageLoad里写

 

         if (!IsPostBack)
 
{
            Response.Write(Request[
"ID"]);
        }

运行下就能看到效果了

希望对初学Url重写的朋友会有所帮助,多多交流啊

如果发现有什么问题,也希望及时给我提出意见,共同进步啦

 

原创粉丝点击