UrlRewrite伪静态


1.首先添加URLRewriter.dll、ActionlessForm.dll加到bin文件夹中,添加引用

 注:URLRewriter.dll实现伪静态  ActionlessForm.dll是分页时起作用,如果不使用该控件仍会跳转到aspx页面

ActionlessForm.dll使用方法:在每个aspx页面添加<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>

 然后,将 <form runat="server">****</form>替换为: <skm:Form id="Form1" method="post" runat="server">*****</skm:Form>

2.按如下(红色部分)操作修改配置文件

<?xml version="1.0"?>
<configuration>
  <configSections>
    <!--URLRewriter_1 URL开始-->
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler,URLRewriter"/>
    <!--URLRewriter_1 URL结束-->

  </configSections>
  <appSettings/>
  <connectionStrings/>
  <!--URLRewriter_2 URL重写开始-->
  <RewriterConfig>
    <Rules>
      <RewriterRule>
        <LookFor>~/(\w*)_(\w*).html</LookFor>
        <SendTo><![CDATA[~/WebForm1.aspx?ID=$1&Page=$2]]></SendTo>
       
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/index.html</LookFor>
        <SendTo>~/Default.aspx</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/(\w*).html</LookFor>
        <SendTo>~/WebForm1.aspx?ID=$1</SendTo>
      </RewriterRule>
    </Rules>
  </RewriterConfig>
  <!--URLRewriter_2 URL重写结束-->

  <system.web>
    <compilation debug="true" targetFramework="4.0">
    </compilation>
    <authentication mode="Windows"/>
    <httpModules>
      <!--URLRewriter_3开始-->
      <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
      <!--URLRewriter_3结束-->

    </httpModules>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
  </system.web>
  <!--
        在 Internet 信息服务 7.0 下运行 ASP.NET AJAX 需要 system.webServer
        节。对早期版本的 IIS 来说则不需要此节。
    -->
  <system.webServer>
    <handlers>
      <remove name="html"/>
      <add name="html" path="*.html" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32"/>
    </handlers>
  </system.webServer>
</configuration>

 

3.程序中转向时调用<LookFor></LookFor>节点中配置的URL,注意参数

4.在iis中添加LookFor 节点配置的后缀

按照*aspx的配置添加映射(此例添加html映射)


付代码:

Default.aspx.cs:

        protected void Page_Load(object sender, EventArgs e)
        {
            RedirectUrl();
        }

        protected void RedirectUrl()
        {
            Response.Redirect("sean_chang.html");
        }

WebForm1.aspx.cs

            if (!IsPostBack)
            {
                Response.Write("实际调用WebForm1.aspx页面,参数ID="+ Request.QueryString["ID"]);

                Response.Write("<script type='text/javascript'>alert('参数page=" + Request.QueryString["Page"] + "')</script>");
            }

Default页面跳转到sean_chang.html页面 实际上访问的是WebForm1.aspx页面 ,并且根据webconfig配置可知 

实际访问的url路径为WebForm1.aspx?ID=sean&Page=chang 从而实现伪静态………………………………………………

页面显示:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值