[英]ASP.NET how to send data with post method?
案子
我有两个Web表单,还有一些代码隐藏。 第一个Web窗体是一个公式编写器,我在其中输入一个字符串。 我想使用post方法将该字符串发送到第二个公式,并显示它。
问题
我收到一条错误消息,说无法验证MAC Viewstate:
应用程序“ /”。 验证MAC Viewstate。 可以在群集中为电池或电池组中的应用程序选择的应用程序,确保验证配置的有效性以及验证算法的有效性。 自动生成群集的新peutpasêtreutiliséedans。
我究竟做错了什么 ?
WebForm1.aspx的
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server" action="WebForm2.aspx" method="post">
<div>
Enter your first name : <input type="text" name="FirstName"/><br />
<input type="submit" />
</div>
</form>
</body>
</html>
Webform2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
First name : <%= FirstName %>
</div>
</form>
</body>
</html>
Webform2.aspx.cs
public partial class WebForm2 : System.Web.UI.Page
{
public string FirstName { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
FirstName = Request.Form["FirstName"];
}
}
注意:我对网络技术的经验很少,我正在尝试学习asp.net和html,请原谅。
编辑1:
WebForm3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<%if (!IsPostBack){ %>
<div>
Enter your first name :
<input type="text" name="FirstName" /><br />
<input type="submit" />
</div>
<% } else { %>
<div>
First name : <%= FirstName %>
</div>
<% } %>
</form>
</body>
</html>
WebForm3.aspx.cs
public partial class WebForm3 : System.Web.UI.Page
{
public string FirstName { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
FirstName = Request.Form["FirstName"];
}
}
}
WebForm1.aspx(修订)
<form id="form1" runat="server" method="post">
<div>
Enter your first name : <input type="text" name="FirstName"/><br />
<asp:Button PostBackUrl="~/WebForm2.aspx" Text="Validate" runat="server"/>
</div>
</form>
WebForm3.aspx正常工作,并且使用asp:Button的WebForm1也正常工作,非常感谢。
附加问题:
该测试的目的是寻找以“通用”方式发送数据的其他方式(邮政方式,因为“他们”表示这是最安全的方式)。 现在,我只剩下一个问题:使用两个文件(WebForm1和WebForm2方法)或单个文件(WebForm3方法)的最佳实践是什么? 换句话说,应该由同一页面负责收集数据并处理它们,还是应该将这些责任分成两个文件?
编辑2,最后一个问题
当使用两个文件时,我看到IsPostBack属性等于false。 使用单个文件时,在提交时,我看到IsPostBack属性等于true。 使用单个文件时,该属性是否仅更改(因此有用)?
WebForm1.aspx的
<form id="form1" runat="server" action="WebForm2.aspx" method="post">
假设我了解您要完成的工作-如果您想覆盖WebForms的PostBack
机制,一种简单的方法是将PostBackUrl
与Button
控件一起使用。
这就是ASP.Net WebForms的工作方式(回发到同一页)。 还有其他发布到另一个WebForms Page的方法 ,但是请首先尝试PostBackUrl
,看看这是否是您真正需要的(阻力最小)。
如果您想使用更“标准化”的工作方式(例如只是想将POST到您选择的资源)作为我想完成的工作,则也可以研究ASP.Net MVC或WebPages。
心连心...
好吧,您不会喜欢我的回答,因为“取决于情况”-
ASP.net WebForms中的“通用方式”实际上是同一页面-默认的PostBack机制,然后您将像上面一样处理显示-通过controls
(例如, Placeholder
或user controls
)。
你也可以看看到的东西像向导控制- >有用的那些,你需要引导用户完成情况Steps
-如可能的应用形式,是漫长的,你想剪成更易于管理的步骤 -就凑齐这件事你自己在单Page
使用Postback
机制,甚至多Page
S(跨页投递),而可行的,可能是在自我惩罚锻炼:)
还有其他方法,但是我不想混淆和关注话题(例如AJAX)。
我将暂缓“安全性”,因为您需要对此进行更详细的说明-也许值得单独提出一个问题。 通常 ,操作规则是“不信任客户端”(意味着验证任何/所有客户端提供您的应用程序的所有数据), 服务器端 -不仅仅是客户端验证(它们应携手工作,而不是相互排斥) 。
心连心...
当使用两个文件时,我看到IsPostBack属性等于false。 使用单个文件时,在提交时,我看到IsPostBack属性等于true。 使用单个文件时,该属性是否仅更改(因此有用)?
IsPostBack
意思是“此POST请求是否来自我自己”?
Button.PostBackUrl
> Page2.aspx = IsPostBack
为false
IsPostBack
任何按钮/控件为true
话虽如此,“仅有用”-在此简单示例中。 但是您可以使用该检查来做其他事情-您之前提到过安全性,因此您可以检查您的页面是否可以/将/应该接受来自任何地方的POST请求。
在您的示例情况下,POST的目标页面(使用2页和PostBackUrl
)应检查/验证其接收的POST数据-因为任何人都可以做您刚做的事情(向它发布POST)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.