利用URLRewriter重写url地址

来源:互联网 发布:c语言中stdio 编辑:程序博客网 时间:2024/04/28 15:39
首先,当然是下载URLRewriter了 download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi  下载安装后再bin目录下找到URLRewriter.dll文件

    然后把这个文件引用到项目中,下面开始配置

   1 在web.config文件中加入如下代码 

<configuration>
  
<configSections>
    
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
  
</configSections>

</configuration>

   其中

<section name="RewriterConfig" 
type
="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>

用于指定配置节"RewriterConfig"的处理程序类的名称为”URLRewriter.Config.RewriterConfigSerializerSectionHandler”,该类存在于bin目录下的URLRewriter .dll文件中

  2 在web.config文件中的system.web节点下加入如下代码

<httpHandlers>
      
<add verb="*" path="*.html"
           type
="URLRewriter.RewriterFactoryHandler, URLRewriter" />
   
</httpHandlers>

    这段代码的意思是:将文件扩展名为 .html 的文件的所有 HTTP 请求映射到类 URLRewriter.RewriterFactoryHandler 具体可以看MSDN,在这里我开始犯了个错误吧path=“*.html”写成了path=“*.aspx”导致了找不到页面,发生404的错误

   3 重写url

        和1一样 ,同样是放在<configuration></configuration>节点下面 

     关键就是

 

复制代码
<RewriterConfig>
    
<Rules>
      
<RewriterRule>
        
<LookFor>~/Shownews/news(\d+)\.html</LookFor>
        
<SendTo>~/Shownews.aspx?ShowID=$1</SendTo>
      
</RewriterRule>
      
<RewriterRule>
        
<LookFor>~/product(\d+)\.html</LookFor>
        
<SendTo>~/Showproduct.aspx?ShowID=$1</SendTo>
      
</RewriterRule>
    
</Rules>
  
</RewriterConfig>
复制代码

 其中关键在uml的转换 

<LookFor>~/Shownews/news(\d+)\.html</LookFor>

<SendTo>~/Shownews.aspx?ShowID=$1</SendTo>

      意思是把第一个路径转成另一个路径。其中<LookFor>()中的正则表达式就是第二句中的参数$1 .

同样也可以用$2 $3来表示<LookFor>中第二 第三个()中的参数。

现在总可以了吧,呵呵。终于看到了,兴奋吧。不要急,这还只是最简单的。如果你的页面有回传。比如说放了DATAGRID,有分页的,你点到下一页就发现,晕倒,又出问题了。
这下怎么办呢,这个其实微软件的网站上就有说到,我在这里简述一下了。

第六步,加入窗体回传保持的组件:
在原来你下载的项目里找到 ActionlessForm.dll 放到你的项目 bin 目录下。
然后在你的这个页面中加入:
<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
再把你的<Form...>改为:
<skm:Form id="你的表单名" method="post" runat="server">
.....
</skm:Form>
That's All.现在你可以高枕无忧了

0 0
原创粉丝点击