最近用vs2010开发的一个网站中,设置了默认页为default.aspx,但只输入网站名的情况下,该页面数据不能正常提交,后台调试代码发现并不执行。进一步发现,如果建立web应用程序时,不存在该问题。为使输入默认网站时能够正常提交数据,增加了Form的Action属性:
1:
<
form
runat
="server"
action
="Default.aspx"
defaultfocus
="username"
>
2:
<
ul
>
3:
<
li
runat
="server"
class
="wrong"
id
="errorMsg"
clientidmode
="Static"
>
</
li
>
4:
<
li
>用户名:
5:
<
input
type
="text"
class
="wid"
id
="username"
name
="username"
runat
="server"
tabindex
="1"
6:
clientidmode
="Static"
/>
7:
</
li
>
8:
<
li
>密
码:
9:
<
input
type
="password"
class
="wid"
id
="password"
name
="password"
runat
="server"
tabindex
="1"
10:
clientidmode
="Static"
/>
11:
<
a
href
="Account/RecoverPwd.aspx"
class
="pw"
>忘记密码
</
a
></
li
>
12:
<
li
class
="font12 mar"
>
13:
<
input
name
="persistentcookie"
id
="rpwd"
type
="checkbox"
value
="1"
runat
="server"
14:
tabindex
="1"
clientidmode
="Static"
/>
15:
<
label
for
="rpwd"
>
16: 记住登录状态
</
label
>
17:
</
li
>
18:
<
li
class
="mar"
>
19:
<
input
id
="login_img"
type
="image"
src
="images/login.gif"
tabindex
="1"
onclick
="return Login();"
/>
20:
<
span
id
="login_tip"
style
="display: none;"
>正在系统,请稍候...
</
span
>
</
li
>
21:
</
ul
>
22:
<
hr
style
="height: 2px; 80%; border-top: 2px dashed #215FD2;"
/>
23:
<
div
style
="font-size: 14px; text-align: center;"
>
24: 还不是会员,
<
a
href
="Account/Register.aspx"
>现在注册
</
a
></
div
>
25:
</
form
>
在添加了Action属性后,可以正常显示了,但当访问该网页中带有查询字符串时,因为提交的网址是default.aspx,是没有后面的查询字符串的,会出现问题,如上面的页面为登录页面,如果在web.config中配置了登录url后,在访问需要登录的页面时,会在页面后带有查询字符串的网址,如:http://localhost/CCUINCom/Default.aspx?ReturnUrl=%2fCCUINCom%2fmanagerCom%2fcustomizeCom.aspx。此时,当登录完成后就不能返回原来的页面。
为了解决这一问题,在页面加载时,指定form的Action属性为请求时的地址,如:
1:
protected
void Page_Load(
object sender, EventArgs e)
2: {
3:
//在只输入网站不输入页面地址时,iis不触发事件,不能正常登录,
4:
//因此,在form中添加了action=default.aspx后,使得因为超时后返回该页面所带的returnUrl参数无效
5:
//因此在页面加载时,得到原始url并将form的action设置成原始地址,以保证正常跳转
6: Form.Action = Request.RawUrl;
7:
string returnUrl = Request.QueryString[
"ReturnUrl"];
8:
if (IsPostBack)
9: {
10: ... ...
11: }
12:
else
13: {
14: ... ...
15: }
16: }
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
此时,可以正常返回登录前的页面了。为此,又得到了一种Asp.net跨页的方式,就是如上种设置form的Action属性,经过实验,页面提交时会转到相应的页面,但会提示viewState验证错误,设置页面的参数,对视图状态不进行验证就可以了,此时可以通过Request的forms属性来访问值 ,这就和asp环境下的方式有些类似了。
转载于:https://blog.51cto.com/xuejie0924/517731