回发不适用于 aspx 页面作为默认文档 [英] Postback doesn't work with aspx page as Default Document

查看:26
本文介绍了回发不适用于 aspx 页面作为默认文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我浏览到 http://localhost/edumatic3/trunk/login/accesscode/Default.aspx,我的回发有效.但是,如果我浏览到 http://localhost/edumatic3/trunk/login/accesscode/(将 Default.aspx 定义为默认文档),我的回发不起作用.

有没有办法让这个工作?或者我应该删除默认文档并强制用户浏览到 http://localhost/edumatic3/中继/登录/访问代码/default.aspx?

更新:

代码(部分):

<asp:ImageButton ID="continueImageButton"runat="服务器" ValidationGroup="继续"OnClick="ContinueImageButton_Click"AlternateText="<%$ Resources:login, continue_alternatetext %>"/>

背后的代码(部分):

protected void Page_Load(object sender, EventArgs e){Log.Debug("Page_Load(...)");Log.Debug("Page_Load(...) :: PostBack = " + IsPostBack);如果 (!IsPostBack){continueImageButton.ImageUrl = "~/App_Themes/" + base.Theme+ "/images/" + Resources.login.btn_continue;}}///<总结>///继续图像按钮点击处理程序///</总结>///<param name="sender"></param>///<param name="e"></param>protected void ContinueImageButton_Click(object sender, EventArgs e){....

当我点击 ImageButton 时,Page_Load 被触发,IsPostBack 为 false...正常情况下应该是 true.ContinueImageButton_Click(...) 根本没有被触发.

在 HTML(部分)中:

Http 请求:

POST/edumatic3/trunk/login/accesscode/HTTP/1.1主机:本地主机推荐人:http://localhost/edumatic3/trunk/login/accesscode/内容长度:1351缓存控制:max-age=0来源:http://localhost用户代理:Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/535.1(KHTML,如 Gecko)Chrome/13.0.782.215 Safari/535.1内容类型:应用程序/x-www-form-urlencoded接受:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8接受编码:gzip、deflate、sdch接受语言:nl,en-US;q=0.8,en;q=0.6,fr;q=0.4接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.3ASP.NET_SessionId=33yal3buv310y2etuj33qghg;CurrenUICulture=en-us__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDw...

解决方案

我想我会尝试重现这个,你说得完全正确.它在没有 default.aspx 的情况下通过您提供的一个非常简单的示例而中断.看看 HTML,原因就很清楚了.这是因为 action 属性为空.

快速搜索发现了这一点,ASP.NET 4 Breaking Changes(请参阅在 IIS 7 或 IIS 7.5 集成模式下的默认文档中可能不会引发事件处理程序).

<块引用>

ASP.NET 4 现在呈现 HTML 表单元素的 action 属性值当对无扩展名的 URL 发出请求时作为空字符串有一个映射到它的默认文档.例如,在早期版本中在 ASP.NET 中,对 http://contoso.com 的请求将导致请求到 Default.aspx.在该文档中,打开表单标签将是如以下示例所示:

在 ASP.NET 4 中,对 http://contoso.com 的请求也会导致对 Default.aspx 的请求.但是,ASP.NET 现在呈现 HTML 开头表单标记如下例所示:

action 属性呈现方式的这种差异会导致IIS 和 ASP.NET 处理表单发布方式的细微变化.当 action 属性为空字符串时,IISDefaultDocumentModule 对象将创建一个子请求默认.aspx.大多数情况下,这个子请求是透明的到应用程序代码,Default.aspx页面正常运行.

但是,托管代码与 IIS 7 或 IIS 之间的潜在交互7.5 集成模式会导致托管 .aspx 页面停止工作在子请求期间正确执行.

我已经创建了这两个修复程序来解决该问题,请使用其中一个.

1) 将此代码添加到 Global.asax

void Application_BeginRequest(object sender, EventArgs e){var app = (HttpApplication)sender;if (app.Context.Request.Url.LocalPath.EndsWith("/")){app.Context.RewritePath(string.Concat(app.Context.Request.Url.LocalPath, "default.aspx"));}}

2) 创建一个表单控件适配器

公共类 FormControlAdapter : ControlAdapter{受保护的覆盖无效渲染(System.Web.UI.HtmlTextWriter writer){base.Render(new RewriteFormHtmlTextWriter(writer));}公共类 RewriteFormHtmlTextWriter : HtmlTextWriter{公共 RewriteFormHtmlTextWriter(HtmlTextWriter writer):基地(作家){this.InnerWriter = writer.InnerWriter;}public override void WriteAttribute(string name, string value,bool fEncode){if (name.Equals("action") && string.IsNullOrEmpty(value)){value = "default.aspx";}base.WriteAttribute(name, value, fEncode);}}}

通过在 App_BrowsersDefault.browsers 中创建这个文件来注册它

<浏览器 refID="默认"><控制适配器><适配器控件类型="System.Web.UI.HtmlControls.HtmlForm"adapterType="TheCodeKing.Web.FormControlAdapter"/></controlAdapters></浏览器></浏览器>

If I browse to http://localhost/edumatic3/trunk/login/accesscode/Default.aspx, my postback works. However, if I browse to http://localhost/edumatic3/trunk/login/accesscode/ (with Default.aspx defined as default document), my postback doesn't work.

Is there a way to make this work? Or should I remove the default document and force users to browse to http://localhost/edumatic3/trunk/login/accesscode/default.aspx?

UPDATE:

Code (part):

<div id="continueDiv">
        <asp:ImageButton ID="continueImageButton" 
                runat="server" ValidationGroup="continue" 
                OnClick="ContinueImageButton_Click" 
                AlternateText="<%$ Resources:login, continue_alternatetext %>"/>
    </div>

Code behind (part):

protected void Page_Load(object sender, EventArgs e)
{
    Log.Debug("Page_Load(...)");
    Log.Debug("Page_Load(...) :: PostBack = " + IsPostBack);

    if (!IsPostBack)
    {
        continueImageButton.ImageUrl = "~/App_Themes/" + base.Theme 
        + "/images/" + Resources.login.btn_continue;
    }
}

/// <summary>
/// Continue Image Button Click Handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ContinueImageButton_Click(object sender, EventArgs e)
{
 ....

When I click on the ImageButton, Page_Load is triggered, and IsPostBack is false... Normally, it should be true. ContinueImageButton_Click(...) isn't triggered at all.

In HTML (part):

<input type="image" name="ctl00$ContentPlaceHolder1$continueImageButton" 
id="ctl00_ContentPlaceHolder1_continueImageButton" 
src="../../App_Themes/LoginTedu/images/en_continue.png" alt="Continue" 
onclick="javascript:WebForm_DoPostBackWithOptions(new 
WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$continueImageButton&quot;, 
&quot;&quot;, true, &quot;continue&quot;, &quot;&quot;, false, false))" 
style="border-width:0px;">

Http request:

POST /edumatic3/trunk/login/accesscode/ HTTP/1.1
Host: localhost
Referer: http://localhost/edumatic3/trunk/login/accesscode/
Content-Length: 1351
Cache-Control: max-age=0
Origin: http://localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 
   (KHTML, like Gecko)                 Chrome/13.0.782.215 Safari/535.1
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: nl,en-US;q=0.8,en;q=0.6,fr;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
ASP.NET_SessionId=33yal3buv310y2etuj33qghg; CurrenUICulture=en-us

__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDw...

解决方案

I thought I'd try and reproduce this, and you're absolutely right. It breaks without the default.aspx with a very simple example that you provided. Looking at the HTML, the reason is fairly clear. It's because the action attribute is empty.

A quick search reveled this, ASP.NET 4 Breaking Changes (see Event Handlers Might Not Be Not Raised in a Default Document in IIS 7 or IIS 7.5 Integrated Mode).

ASP.NET 4 now renders the HTML form element’s action attribute value as an empty string when a request is made to an extensionless URL that has a default document mapped to it. For example, in earlier releases of ASP.NET, a request to http://contoso.com would result in a request to Default.aspx. In that document, the opening form tag would be rendered as in the following example:

<form action="Default.aspx" />

In ASP.NET 4, a request to http://contoso.com also results in a request to Default.aspx. However, ASP.NET now renders the HTML opening form tag as in the following example:

<form action="" />

This difference in how the action attribute is rendered can cause subtle changes in how a form post is processed by IIS and ASP.NET. When the action attribute is an empty string, the IIS DefaultDocumentModule object will create a child request to Default.aspx. Under most conditions, this child request is transparent to application code, and the Default.aspx page runs normally.

However, a potential interaction between managed code and IIS 7 or IIS 7.5 Integrated mode can cause managed .aspx pages to stop working properly during the child request.

I've created these two fixes which resolve the issue, use either.

1) Add this code to Global.asax

void Application_BeginRequest(object sender, EventArgs e)
{
    var app = (HttpApplication)sender;
    if (app.Context.Request.Url.LocalPath.EndsWith("/"))
    {
    app.Context.RewritePath(
             string.Concat(app.Context.Request.Url.LocalPath, "default.aspx"));
    }
}

2) Create a Forms ControlAdapter

public class FormControlAdapter : 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)
        {
            this.InnerWriter = writer.InnerWriter;
        }

        public override void WriteAttribute(string name, string value,
                                            bool fEncode)
        {
            if (name.Equals("action") && string.IsNullOrEmpty(value))
            {
                value = "default.aspx";
            }
            base.WriteAttribute(name, value, fEncode);
        }
    }
}

Register it by creating this file in App_BrowsersDefault.browsers

<browsers>
    <browser refID="Default">
       <controlAdapters>
          <adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
                            adapterType="TheCodeKing.Web.FormControlAdapter" />
       </controlAdapters>
    </browser>
</browsers>

这篇关于回发不适用于 aspx 页面作为默认文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆