.net - 回发不适用于 aspx 页面作为默认文档

标签 .net asp.net iis

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

有办法让这个工作成功吗?或者我应该删除默认文档并强制用户浏览到 http://localhost/edumatic3/trunk/login/accesscode/default.aspx

更新:

代码(部分):

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

隐藏代码(部分):

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)
{
 ....

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

在 HTML 中(部分):

<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 请求:

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...

最佳答案

我想我应该尝试重现这个,你是完全正确的。如果没有 default.aspx ,它会因您提供的一个非常简单的示例而中断。看看 HTML,原因就很清楚了。这是因为action属性为空。

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

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.

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

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)创建表单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);
        }
    }
}

通过在 App_Browsers\Default.browsers 中创建此文件来注册它

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

关于.net - 回发不适用于 aspx 页面作为默认文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7228344/

相关文章:

c# - 数据网格 asp.net 中的超链接列

iis - MSDeploy 通过 FTP 发布

c# - 当添加的 MIME 类型不适用于 .Net Core 项目的 IIS 时如何进行调试

c# - 从构建输出中省略程序集的本地化版本

.net - 如何检测加载图像是否会在 .NET 中引发 OutOfMemory 异常?

c# - 约束引用 'string'无法解析为类型。 (netcoreapp3.0)

c# - 我可以获得在 MySQL 中导致重复异常的特定字段吗?

iis - 重置 IIS 后出现 WIF RTM 的奇怪错误

c# - 保持VS2005与文件系统同步?

c# - RabbitMQ - 主题交换和竞争消费者