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.
如果我浏览到http://localhost/edumatic3/trunk/login/accesscode/Default。aspx,我回发。但是,如果我浏览到http://localhost/edumatic3/trunk/login/accesscode/(默认情况下)。aspx定义为默认文档),我的回发不起作用。
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?
有什么办法可以让它工作吗?或者,我应该删除默认文档并强制用户浏览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.
当我点击ImageButton时,Page_Load被触发,而IsPostBack是假的…通常情况下,这应该是真的。ContinueImageButton_Click(…)根本没有被触发。
In HTML (part):
在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("ctl00$ContentPlaceHolder1$continueImageButton",
"", true, "continue", "", false, false))"
style="border-width:0px;">
Http request:
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...
8 个解决方案
#1
44
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.
我想我会试着复制一下,你说得对。它在没有默认的情况下就会崩溃。aspx提供了一个非常简单的示例。看一下HTML,原因很清楚。因为action属性是空的。
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中断更改(请参见在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:
ASP。NET 4现在将HTML表单元素的动作属性值呈现为空字符串,当对具有默认文档映射的无扩展URL发出请求时。例如,在早期的ASP版本中。NET,对http://contoso.com的请求将导致对Default.aspx的请求。在该文件中,开始的表单标记将呈现如下示例所示:
<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:
在ASP。NET 4中,对http://contoso.com的请求也会导致对Default.aspx的请求。然而,ASP。NET现在呈现HTML开始表单标记,如下例所示:
<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.
操作属性的呈现方式的差异会导致IIS和ASP.NET处理表单post的方式发生微妙的变化。当action属性为空字符串时,IIS DefaultDocumentModule对象将创建一个对Default.aspx的子请求。在大多数情况下,这个子请求对应用程序代码是透明的,并且是默认的。aspx页面运行正常。
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.
但是,托管代码与IIS 7或IIS 7.5集成模式之间的潜在交互可能导致托管.aspx页面在子请求期间停止正常工作。
I've created these two fixes which resolve the issue, use either.
我已经创建了这两个解决问题的修复程序。
1) Add this code to Global.asax
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) Create a Forms ControlAdapter
2)创建一个表单控件
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_Browsers\Default.browsers
通过在app_browser \ default .browser中创建这个文件来注册它
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
adapterType="TheCodeKing.Web.FormControlAdapter" />
</controlAdapters>
</browser>
</browsers>
#2
14
Another option, is to check if the form action is empty right before rendering the page. This worked for me:
另一个选项是在呈现页面之前检查表单动作是否为空。这工作对我来说:
public void Page_PreRender(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.Page.Form.Action))
this.Page.Form.Action = "Default.aspx";
}
#3
7
If you are interested in adding some extra code in your default.aspx file, then you can use the similar approach defined in blog post here; Which is about redirecting the user to same default page but with explicit page name....
如果您有兴趣在默认情况下添加一些额外的代码。aspx文件,然后您可以使用类似的方法定义在博客文章这里;大概是重定向用户相同的默认页面但明确的页面名称....
//code, copied from mentioned blog
//代码,从上面提到的博客中复制
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);
}
}
#4
1
Have you tried setting your image button to use the Command event rather than 'Click'?
您是否尝试过将图像按钮设置为使用命令事件而不是“单击”?
I think it 'could' be possible that an image click doesn't cause a full postback, perhaps try defining things as below:
我认为“有可能”图片点击不会引起完整的回发,或许可以尝试定义如下内容:
void ImageButton_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "YourCommandName")
//do your action
}
Then define your button like this:
然后这样定义你的按钮:
<asp:ImageButton id="btn1" runat="server"
ImageUrl="images/image.jpg"
OnCommand="ImageButton_Command"
CommandName="YourCommandName"/>
I seem to remember needing to do this to allow an image button to 'submit' a form, so I presume this will cause the postback you're after.
我似乎记得需要这样做,以允许图像按钮“提交”表单,所以我认为这将导致您正在进行的回发。
#5
0
I once had a similar problem. The problem was that in IIS the default document was configured as default.aspx, but the name of my page was Default.aspx. It was just a matter of case sensitivity.
我曾经遇到过类似的问题。问题是,在IIS中,默认文档被配置为默认文档。但我的页面名是Default.aspx。这只是病例敏感性的问题。
#6
0
When I upgraded my web project from VS 2005 (.Net 2.5) to VS2010 (.Net 4.0), the following was inserted by VS2010 into my web.config:
当我升级我的web项目从VS 2005(。Net 2.5到VS2010(。Net 4.0), VS2010将以下内容插入我的web.config:
<system.webServer>
<defaultDocument>
<files>
<add value="mydefaultpage.aspx" />
</files>
</defaultDocument>
</system.webServer>
When I navigated to "http://myserver/mywebsite", which I was formerly able to do under .Net 2.5, I got
当我导航到“http://myserver/mywebsite”(我以前可以在。net 2.5下进行)时,我得到了
"HTTP Error 500.19 - Internal Server Error "The requested page cannot be accessed because the related configuration data for the page is invalid." (It displayed the "defaultDocument" node.)
“HTTP错误500.19 -内部服务器错误”请求的页面不能被访问,因为页面的相关配置数据无效。(它显示了“defaultDocument”节点。)
However, I was able to fix the problem very simply in web.config simply by inserting "/" at the beginning of the default web page value, as shown below:
然而,我能够在web上很简单地解决这个问题。配置只需在默认web页面值的开头插入“/”,如下所示:
<system.webServer>
<defaultDocument>
<files>
<add value="/mydefaultpage.aspx" />
</files>
</defaultDocument>
</system.webServer>
I didn't have to do any of the other things suggested by the other respondents.
我不需要做其他受访者建议的任何其他事情。
Are there any unintended consequences or gotchas from doing it this way?
这样做是否会产生意想不到的后果?
#7
0
If your form processor action is not in the root you should be able to use an absolute path without using the file name.
如果您的表单处理器操作不在根目录中,那么您应该能够使用绝对路径而不用文件名。
<form action='/form/processor/' method=post id='myform'>
Tested in .NET 2.0
测试了在。net 2.0
The issue occurred with blank action path, but worked correctly with path entry.
问题出现在空白操作路径中,但在路径条目中正确工作。
#8
0
Consider using URL Mappings done through your web.config. That way you can avoid extra code in your app and let IIS work for you.
考虑使用通过web.config完成的URL映射。这样你就可以避免应用程序中的额外代码,让IIS为你工作。
<system.web>
<urlMappings>
<add url="~/login" mappedUrl="~/login/default.aspx" />
<add url="~/login/" mappedUrl="~/login/default.aspx" />
<add url="~/this-folder-does-not-exist" mappedUrl="~/login/default.aspx" />
<add url="~/this-folder-does-not-exist/" mappedUrl="~/login/default.aspx" />
</urlMappings>
</system.web>
Make sure to include both versions of that URL with and without ending slash.
请确保包含该URL的两个版本,包含和不包含斜杠。
You can also create redirections from virtual folders when you need quick changes and don't want to modify the source code (as shown above with 'this-folder-does-not-exist' example).
当您需要快速更改并且不希望修改源代码时,您也可以从虚拟文件夹创建重定向(如上面所示的“this-folder-does-not-exist”示例)。
#1
44
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.
我想我会试着复制一下,你说得对。它在没有默认的情况下就会崩溃。aspx提供了一个非常简单的示例。看一下HTML,原因很清楚。因为action属性是空的。
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中断更改(请参见在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:
ASP。NET 4现在将HTML表单元素的动作属性值呈现为空字符串,当对具有默认文档映射的无扩展URL发出请求时。例如,在早期的ASP版本中。NET,对http://contoso.com的请求将导致对Default.aspx的请求。在该文件中,开始的表单标记将呈现如下示例所示:
<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:
在ASP。NET 4中,对http://contoso.com的请求也会导致对Default.aspx的请求。然而,ASP。NET现在呈现HTML开始表单标记,如下例所示:
<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.
操作属性的呈现方式的差异会导致IIS和ASP.NET处理表单post的方式发生微妙的变化。当action属性为空字符串时,IIS DefaultDocumentModule对象将创建一个对Default.aspx的子请求。在大多数情况下,这个子请求对应用程序代码是透明的,并且是默认的。aspx页面运行正常。
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.
但是,托管代码与IIS 7或IIS 7.5集成模式之间的潜在交互可能导致托管.aspx页面在子请求期间停止正常工作。
I've created these two fixes which resolve the issue, use either.
我已经创建了这两个解决问题的修复程序。
1) Add this code to Global.asax
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) Create a Forms ControlAdapter
2)创建一个表单控件
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_Browsers\Default.browsers
通过在app_browser \ default .browser中创建这个文件来注册它
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
adapterType="TheCodeKing.Web.FormControlAdapter" />
</controlAdapters>
</browser>
</browsers>
#2
14
Another option, is to check if the form action is empty right before rendering the page. This worked for me:
另一个选项是在呈现页面之前检查表单动作是否为空。这工作对我来说:
public void Page_PreRender(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.Page.Form.Action))
this.Page.Form.Action = "Default.aspx";
}
#3
7
If you are interested in adding some extra code in your default.aspx file, then you can use the similar approach defined in blog post here; Which is about redirecting the user to same default page but with explicit page name....
如果您有兴趣在默认情况下添加一些额外的代码。aspx文件,然后您可以使用类似的方法定义在博客文章这里;大概是重定向用户相同的默认页面但明确的页面名称....
//code, copied from mentioned blog
//代码,从上面提到的博客中复制
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);
}
}
#4
1
Have you tried setting your image button to use the Command event rather than 'Click'?
您是否尝试过将图像按钮设置为使用命令事件而不是“单击”?
I think it 'could' be possible that an image click doesn't cause a full postback, perhaps try defining things as below:
我认为“有可能”图片点击不会引起完整的回发,或许可以尝试定义如下内容:
void ImageButton_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "YourCommandName")
//do your action
}
Then define your button like this:
然后这样定义你的按钮:
<asp:ImageButton id="btn1" runat="server"
ImageUrl="images/image.jpg"
OnCommand="ImageButton_Command"
CommandName="YourCommandName"/>
I seem to remember needing to do this to allow an image button to 'submit' a form, so I presume this will cause the postback you're after.
我似乎记得需要这样做,以允许图像按钮“提交”表单,所以我认为这将导致您正在进行的回发。
#5
0
I once had a similar problem. The problem was that in IIS the default document was configured as default.aspx, but the name of my page was Default.aspx. It was just a matter of case sensitivity.
我曾经遇到过类似的问题。问题是,在IIS中,默认文档被配置为默认文档。但我的页面名是Default.aspx。这只是病例敏感性的问题。
#6
0
When I upgraded my web project from VS 2005 (.Net 2.5) to VS2010 (.Net 4.0), the following was inserted by VS2010 into my web.config:
当我升级我的web项目从VS 2005(。Net 2.5到VS2010(。Net 4.0), VS2010将以下内容插入我的web.config:
<system.webServer>
<defaultDocument>
<files>
<add value="mydefaultpage.aspx" />
</files>
</defaultDocument>
</system.webServer>
When I navigated to "http://myserver/mywebsite", which I was formerly able to do under .Net 2.5, I got
当我导航到“http://myserver/mywebsite”(我以前可以在。net 2.5下进行)时,我得到了
"HTTP Error 500.19 - Internal Server Error "The requested page cannot be accessed because the related configuration data for the page is invalid." (It displayed the "defaultDocument" node.)
“HTTP错误500.19 -内部服务器错误”请求的页面不能被访问,因为页面的相关配置数据无效。(它显示了“defaultDocument”节点。)
However, I was able to fix the problem very simply in web.config simply by inserting "/" at the beginning of the default web page value, as shown below:
然而,我能够在web上很简单地解决这个问题。配置只需在默认web页面值的开头插入“/”,如下所示:
<system.webServer>
<defaultDocument>
<files>
<add value="/mydefaultpage.aspx" />
</files>
</defaultDocument>
</system.webServer>
I didn't have to do any of the other things suggested by the other respondents.
我不需要做其他受访者建议的任何其他事情。
Are there any unintended consequences or gotchas from doing it this way?
这样做是否会产生意想不到的后果?
#7
0
If your form processor action is not in the root you should be able to use an absolute path without using the file name.
如果您的表单处理器操作不在根目录中,那么您应该能够使用绝对路径而不用文件名。
<form action='/form/processor/' method=post id='myform'>
Tested in .NET 2.0
测试了在。net 2.0
The issue occurred with blank action path, but worked correctly with path entry.
问题出现在空白操作路径中,但在路径条目中正确工作。
#8
0
Consider using URL Mappings done through your web.config. That way you can avoid extra code in your app and let IIS work for you.
考虑使用通过web.config完成的URL映射。这样你就可以避免应用程序中的额外代码,让IIS为你工作。
<system.web>
<urlMappings>
<add url="~/login" mappedUrl="~/login/default.aspx" />
<add url="~/login/" mappedUrl="~/login/default.aspx" />
<add url="~/this-folder-does-not-exist" mappedUrl="~/login/default.aspx" />
<add url="~/this-folder-does-not-exist/" mappedUrl="~/login/default.aspx" />
</urlMappings>
</system.web>
Make sure to include both versions of that URL with and without ending slash.
请确保包含该URL的两个版本,包含和不包含斜杠。
You can also create redirections from virtual folders when you need quick changes and don't want to modify the source code (as shown above with 'this-folder-does-not-exist' example).
当您需要快速更改并且不希望修改源代码时,您也可以从虚拟文件夹创建重定向(如上面所示的“this-folder-does-not-exist”示例)。