自定义表单控件适配器未触发

时间:2012-02-17 17:25:58

标签: c# asp.net control-adapter

我有一个网站,其中包含一个自定义控件适配器,用于重写表单上的操作。 (这是我们从其他代理商继承的网站)在调试模式下在localhost上运行时,一切都很好。但是,只要我在QA环境中将站点发布到服务器,自定义适配器就会停止触发。

我发布了调试符号并附加到w3wp进程。单步执行页面加载过程,自定义控件适配器中设置的断点永远不会被命中。当您在localhost上运行调试时,它们会在每次页面加载时触发。生产代码在同一服务器上运行,生产中的自定义控件适配器正在运行。但是,我们需要对生产进行更新,我们担心会破坏生产网站。

有没有人知道为什么自定义控件适配器不会在服务器上触发?我以前从未使用过自定义适配器,除了将适配器放在App_Code文件夹和App_Browser文件夹中的.browser文件中的条目之外,还有什么需要做的吗?

App_Code文件夹中的适配器代码:

public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter {

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

}

public class RewriteFormHtmlTextWriter : HtmlTextWriter {
    public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
        : base(writer) {
        InnerWriter = writer.InnerWriter;
    }
    public RewriteFormHtmlTextWriter(TextWriter writer)
        : base(writer) {
        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) {
                // Because we are using the UrlRewriting.net HttpModule, we will use the 
                // Request.RawUrl property within ASP.NET to retrieve the origional URL
                // before it was re-written.  You//ll want to change the line of code below
                // if you use a different URL rewriting implementation.

                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);
    }
}

我的.browser文件位于App_Browser文件夹中:

<browsers>

  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="FormRewriterControlAdapter" />
      <!--<adapter controlType="System.Web.UI.HtmlControls.HtmlLink" adapterType="LinkRewriterControlAdapter" />-->
    </controlAdapters>
  </browser>

</browsers>

0 个答案:

没有答案