zoukankan      html  css  js  c++  java
  • 利用URLRewriter重写url地址

    首先,当然是下载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.现在你可以高枕无忧了

  • 相关阅读:
    博客园的自定义皮肤
    为自己的审美观感到惭愧
    关于GitHub的Hello Word
    使用Windows Live Writer撰写的第一篇博文
    正式入驻博客园了
    一个使用 Web Components 的音乐播放器: MelodyPlayer
    一个(伪)MaterialDesign风格的博客园皮肤
    从零开始,做一个NodeJS博客(一):Heroku上的最简NodeJS服务器
    从零开始,做一个NodeJS博客(零):整体规(chui)划(niu)
    在 Xamarin.Android 中使用 Notification.Builder 构建通知
  • 原文地址:https://www.cnblogs.com/xuan52rock/p/4626600.html
Copyright © 2011-2022 走看看