【问题标题】:ASP.NET Multiple Page_Load events with URL Rewriting带有 URL 重写的 ASP.NET 多个 Page_Load 事件
【发布时间】:2010-02-18 16:17:05
【问题描述】:

我使用intelligencia urlrewriter 作为我的url 重写模块。我有一个非常奇怪的问题,它只在重写 url 时发生,但为了让它更有趣,而不是在所有重写的页面上。

编辑:忘记告诉你出了什么问题boing boing。问题是我的 Page_Load 事件被触发了 2 次。

这就是我的表单重写适配器的样子:

using System;

使用 System.Web.UI; 使用 System.Web; 使用 System.Web.UI.WebControls;

公共类 FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter {

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{

    base.Render(new RewriteFormHtmlTextWriter(writer));

}

}

公共类 RewriteFormHtmlTextWriter : HtmlTextWriter {

public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
    : base(writer)
{
    this.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 = default(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);

}

}

这就是我的 web.config 的样子

        <!-- Here the double page_load occurs  --> 
    <rewrite url="~/car-parts/(\d+)/(.+)" to="~/Products.aspx?type=parts&amp;iid=$1&amp;cid=9" />
    <rewrite url="~/car-stereo/(\d+)/(.+)" to="~/Products.aspx?type=stereo&amp;iid=$1&amp;cid=10" />



    <!-- this is working correctly -->
     <rewrite url="~/car-parts/browse-by-type/(\d+)/(.+)/(\d+)/(\d+)" to="~/Browse.aspx?cid=9&amp;type=country&amp;countryid=$1&amp;p=$3&amp;filter=$4" />

我不知道去哪里找了,我检查了我的 html 标记,因为我读过这可能会导致这个问题。

亲切的问候, 标记

【问题讨论】:

  • 你没有说一个非常奇怪的问题是什么。

标签: asp.net postback rewrite pageload


【解决方案1】:

当我在我的规则中使用这个重写规则时,这个问题已经解决了:

<rewrite url="^(/.+(\.gif|\.flv|\.swf|\.png|\.jpg|\.ico|\.pdf|\.doc|\.xls|\.css|\.zip|\.rar|\.js|\.xml|\.mp3)(\?.+)?)$" to="$1" processing="stop" />

但请记住在所有 .css/.js/.jpg/... 规则之后使用此规则。

【讨论】:

    【解决方案2】:

    终于找到了,和rewrite模块没有任何关系,这是导致问题的原因:

    在我的一个用户控件中,我使用了 updateprogress

        <asp:UpdateProgress runat="server" AssociatedUpdatePanelID="upNewsletter" DisplayAfter="0">
        <ProgressTemplate>
            <asp:Image runat="server" ID="imgLoading" ImageUrl="~/Images/Template/loading.gif" />
        </ProgressTemplate>
    </asp:UpdateProgress>
    

    这就是问题所在,在 asp:Image 标记中。我已经用常规的 img 标签替换了它,现在一切都正常了。我花了一些时间来解决这个问题,我希望我能避免你的头疼。

    亲切的问候

    【讨论】:

      【解决方案3】:

      我发现这篇文章正在搜索多个 page_loads,但我在使用动态创建的图像的动态创建的 CollapsePanel 时遇到了问题。通过将 ImageUrl 填充为默认图像,问题就解决了。

      header.Controls.Add( new Image
      {
          ID = string.Format( "headerImage_{0}",  panelId ),
          EnableViewState = false,
          ImageUrl = "~/Images/collapse.jpg"
      } );
      

      【讨论】:

        猜你喜欢
        • 2011-12-26
        • 2011-08-26
        • 1970-01-01
        • 1970-01-01
        • 2011-11-04
        • 1970-01-01
        • 2018-06-07
        • 2018-02-21
        • 1970-01-01
        相关资源
        最近更新 更多