Asp.net url重写后页面回发地址改变的处理

在Url重写的页面中进行PostBack并且不跳转的情况,会导致URL变回URL重写前的原始URL,这样的体验不太好,但是这种请况可以通过以下方法进行处理:

在微软的URLRewriter类库中添加以下类之后编译后,然后在项目中引用该UrlRewriter.dll文件

代码 


Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// FormRewriter 的摘要说明
/// </summary>
namespace URLRewriter
{
    public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
    {
        public FormRewriterControlAdapter()
        {
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(new RewriteFormHtmlTextWriter(writer));
        }
    }

    public class RewriteFormHtmlTextWriter : HtmlTextWriter
    {
        public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
            : base(writer)
        {
            base.InnerWriter = writer.InnerWriter;
        }
        public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
            : base(writer)
        {
            base.InnerWriter = writer;
        }

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            //If the attribute we are writing is the "action" attribute, and we are not on a sub-control, 
            //then replace the value to write with the raw URL of the request - which ensures that we'll
            //preserve the PathInfo value on postback scenarios
            if (name == "action")
            {
                HttpContext context = HttpContext.Current;
                if (context.Items["ActionAlreadyWritten"] == null)
                {
                    //We will use the Request.RawUrl property within ASP.NET to retrieve the origional 
                    //URL before it was re-written.
                    value = context.Request.RawUrl;
                    //Indicate that we've already rewritten the <form>'s action attribute to prevent
                    //us from rewriting a sub-control under the <form> control
                    context.Items["ActionAlreadyWritten"] = true;
                }
            }
            base.WriteAttribute(name, value, fEncode);
        }
    }

}

URLRewriter类库代码可以在UrlRewriter下载

然后在App_Browsers文件夹下创建Form.browser

代码如下:

<!--
可在 <windir>\Microsoft.NET\Framework\<ver>\CONFIG\Browsers 中找到现有的浏览器定义
-->
<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
               adapterType="URLRewriter.FormRewriterControlAdapter" />
    </controlAdapters>
  </browser>
  
    <browser id="NewBrowser" parentID="Mozilla">
        <identification>
            <userAgent match="Unique User Agent Regular Expression" />
        </identification>

        <capture>
            <userAgent match="NewBrowser (?'version'\d+\.\d+)" />
        </capture>

        <capabilities>
            <capability name="browser" value="My New Browser" />
            <capability name="version" value="${version}" />
        </capabilities>
    </browser>

    <browser refID="Mozilla">
        <capabilities>
            <capability name="xml" value="true" />
        </capabilities>
    </browser> 
</browsers>

 

在处理重写成html的时候本来网站中的html页面将会不能使用

使用以上方式将不存在这个个问题

如果还不行可以在<compilation debug="true">节点下添加

<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>

在<httpHandlers>节点下添加(如果之前使用的是http处理程序执行重写的,请写在前面)

<add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>

 

转载于:https://www.cnblogs.com/bianlan/archive/2012/09/02/2667603.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值