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

Lie*_*oen 39 .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>
Run Code Online (Sandbox Code Playgroud)

代码背后(部分):

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)
{
 ....
Run Code Online (Sandbox Code Playgroud)

当我单击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;">
Run Code Online (Sandbox Code Playgroud)

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...
Run Code Online (Sandbox Code Playgroud)

The*_*ing 47

我以为我会尝试重现这一点,你是绝对正确的.它没有default.aspx您提供的一个非常简单的示例而中断.看看HTML,原因很清楚.这是因为action属性为空.

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

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

<form action="Default.aspx" />
Run Code Online (Sandbox Code Playgroud)

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

<form action="" />
Run Code Online (Sandbox Code Playgroud)

动作属性的呈现方式的这种差异可能会导致IIS和ASP.NET处理表单帖子的方式发生细微变化.当action属性为空字符串时,IIS DefaultDocumentModule对象将创建对Default.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"));
    }
}
Run Code Online (Sandbox Code Playgroud)

2)创建一个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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

<browsers>
    <browser refID="Default">
       <controlAdapters>
          <adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
                            adapterType="TheCodeKing.Web.FormControlAdapter" />
       </controlAdapters>
    </browser>
</browsers>
Run Code Online (Sandbox Code Playgroud)

  • 这很好,但有没有办法配置IIS来修复它,而不是必须进行代码更改?闻起来像是一个IIS的bug,或者只是MS的糟糕设计. (2认同)

小智 14

另一个选项是在呈现页面之前检查表单操作是否为空.这对我有用:

    public void Page_PreRender(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.Page.Form.Action))
            this.Page.Form.Action = "Default.aspx";
    }
Run Code Online (Sandbox Code Playgroud)


Waq*_*qas 7

如果你有兴趣在你的Default.aspx文件中添加一些额外的代码,那么你可以使用在博客文章中定义的类似的方法在这里 ; 这是关于将用户重定向到相同的默认页面,但具有明确的页面名称....

//代码,从提到的博客复制而来

protected void Page_Load(object sender, EventArgs e)
{        
    string defaultPage = "default.aspx";
    string rawUrl = Request.RawUrl; //get current url

    //if current url doesn't contains default page name then add
    //default page name, and append query string as it is, if any
    if (rawUrl.ToLower().IndexOf(defaultPage) < 0)
    {
        string newUrl;
        if (rawUrl.IndexOf("?") >= 0)
        {
            // URL contains query string
            string[] urlParts = rawUrl.Split("?".ToCharArray(), 2);

            newUrl = urlParts[0] + defaultPage + "?" + urlParts[1];
        }
        else
        {
            newUrl = (rawUrl.EndsWith("/")) ? rawUrl + defaultPage : rawUrl + "/" + defaultPage;
        }

        Response.Redirect(newUrl);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个很好的解决方法,但你看到的不是正常行为IMO,它应该只是工作.您的环境搞砸了,或者还有其他一些因素在起作用. (2认同)