如何将FORM从HTML发布到ASPX页面

时间:2022-10-16 16:30:09

How do I post a form from an HTML page to and ASPX page (2.0) and be able to read the values?

如何将表单从HTML页面发布到ASPX页面(2.0)并能够读取值?

I currently have an ASP.NET site using the Membership provider and everything is working fine. Users can log in from the Login.aspx page.

我目前有一个使用成员资格提供程序的ASP.NET站点,一切正常。用户可以从Login.aspx页面登录。

We now want to be able to have users log in directly from another web site--which is basically a static HTML page. The users need to be able to enter their name and password on this HTML page and have it POST to my Login.aspx page (where I can then log them in manually).

我们现在希望能够让用户直接从另一个网站登录 - 这基本上是一个静态HTML页面。用户需要能够在此HTML页面上输入他们的名称和密码,并将其POST到我的Login.aspx页面(我可以在其中手动登录)。

Is it possible to pass form values from HTML to ASPX? I have tried everything and the Request.Form.Keys collection is always empty. I can't use a HTTP GET as these are credentials and can't be passed on a query string.

是否可以将表单值从HTML传递到ASPX?我已经尝试了一切,Request.Form.Keys集合总是空的。我不能使用HTTP GET,因为这些是凭据,不能在查询字符串上传递。

The only way I know of is an iframe.

我所知道的唯一方法是iframe。

7 个解决方案

#1


27  

This is very possible. I mocked up 3 pages which should give you a proof of concept:

这是非常可能的。我嘲笑了3页,应该给你一个概念证明:

.aspx page:

.aspx页面:

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox TextMode="password" ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
</form>

code behind:

代码背后:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For Each s As String In Request.Form.AllKeys
        Response.Write(s & ": " & Request.Form(s) & "<br />")
    Next
End Sub

Separate HTML page:

单独的HTML页面:

<form action="http://localhost/MyTestApp/Default.aspx" method="post">
    <input name="TextBox1" type="text" value="" id="TextBox1" />
    <input name="TextBox2" type="password" id="TextBox2" />
    <input type="submit" name="Button1" value="Button" id="Button1" />
</form>

...and it regurgitates the form values as expected. If this isn't working, as others suggested, use a traffic analysis tool (fiddler, ethereal), because something probably isn't going where you're expecting.

......并按预期反刍形式值。如果这不起作用,正如其他人建议的那样,使用流量分析工具(fiddler,ethereal),因为某些东西可能不会出现在您期望的位置。

#2


18  

The Request.Form.Keys collection will be empty if none of your html inputs have NAMEs. It's easy to forget to put them there after you've been doing .NET for a while. Just name them and you'll be good to go.

如果您的html输入都没有NAME,则Request.Form.Keys集合将为空。在你做了一段时间的.NET之后,很容易忘记将它们放在那里。只需命名它们就可以了。

#3


2  

Are you sure your HTML form is correct, and does, in fact, do an HTTP POST? I would suggest running Fiddler2, and then trying to log in via your Login.aspx, then the remote HTML site, and then comparing the requests that are sent to the server. For me, ASP.Net always worked fine -- if HTTP request contains a valid POST, I can get to values using Request.Form...

您确定您的HTML表单是否正确,并且实际上是否进行了HTTP POST?我建议运行Fiddler2,然后尝试通过您的Login.aspx,然后远程HTML站点登录,然后比较发送到服务器的请求。对我来说,ASP.Net总是工作正常 - 如果HTTP请求包含有效的POST,我可以使用Request.Form获取值...

#4


1  

You sure can.

你确定可以。

The easiest way to see how you might do this is to browse to the aspx page you want to post to. Then save the source of that page as HTML. Change the action of the form on your new html page to point back to the aspx page you originally copied it from.

了解如何执行此操作的最简单方法是浏览到要发布到的aspx页面。然后将该页面的源保存为HTML。更改新html页面上表单的操作,以指回最初复制它的aspx页面。

Add value tags to your form fields and put the data you want in there, then open the page and hit the submit button.

将值标签添加到表单字段并将所需数据放入其中,然后打开页面并单击“提交”按钮。

#5


1  

You sure can. Create an HTML page with the form in it that will contain the necessary components from the login.aspx page (i.e. username, etc), and make sure they have the same IDs. For you action, make sure it's a post.

你确定可以。创建一个包含表单的HTML页面,其中包含login.aspx页面中的必要组件(即用户名等),并确保它们具有相同的ID。为了你的行动,请确保它是一个帖子。

You might have to do some code on the login.aspx page in the Page_Load function to read the form (in the Request.Form object) and call the appropriate functions to log the user in, but other than that, you should have access to the form, and can do what you want with it.

您可能必须在Page_Load函数的login.aspx页面上执行一些代码来读取表单(在Request.Form对象中)并调用相应的函数来记录用户,但除此之外,您应该有权访问形式,并可以做你想要的。

#6


1  

In the html form, you need to supply additional viewstate variable and disable ViewState in a server page. This requires some control on both sides , though.

在html表单中,您需要提供额外的viewstate变量并在服务器页面中禁用ViewState。但这需要对双方进行一些控制。

Form HTML:

表单HTML:

<html><body> <form id='postForm' action='WebForm.aspx' method='POST'>
        <input type='text' name='postData' value='base-64-encoded-value' />
        <input type='hidden' name='__VIEWSTATE' value='' />  <!-- still need __VIEWSTATE, even empty one -->
    </form>

</body></html>

Note empty __VIEWSTATE.

注意空__VIEWSTATE。

WebForm.aspx:

WebForm.aspx:

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm.aspx.cs" Inherits="WebForm"
 EnableEventValidation="False" EnableViewState="false" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="postForm" runat="server">
        <asp:TextBox ID="postData" runat="server"></asp:TextBox>
    <div>

    </div>
    </form>
</body>
</html>

Note EnableEventValidation="False", EnableViewState="false" to prevent validation error for empty view state. Code Behind/Inherits values are not precise.

注意EnableEventValidation =“False”,EnableViewState =“false”以防止空视图状态的验证错误。 Code Behind / Inherits值不准确。

WebForm.cs:

WebForm.cs:

public partial class WebForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string value = Encoding.Unicode.GetString(Convert.FromBase64String(this.postData.Text));
    }
}

#7


0  

Hope this will help - Put this tag in html and

希望这会有所帮助 - 把这个标签放在html和

remove your login.aspx design content..just write only page directive

删除你的login.aspx设计内容..只写页面指令

and you will get the values in aspx page after submit button click like this- protected void Page_Load(object sender, EventArgs e) {

并且您将在提交按钮单击后获取aspx页面中的值,如此受保护的void Page_Load(object sender,EventArgs e){

        if (!IsPostBack)
        {
            CompleteRegistration();
        }
    }

public void CompleteRegistration() {

public void CompleteRegistration(){

        NameValueCollection nv = Request.Form;
        if (nv.Count != 0)
        {
            string strname = nv["txtbox1"];
            string strPwd = nv["txtbox2"];
        }
    }

#1


27  

This is very possible. I mocked up 3 pages which should give you a proof of concept:

这是非常可能的。我嘲笑了3页,应该给你一个概念证明:

.aspx page:

.aspx页面:

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox TextMode="password" ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
</form>

code behind:

代码背后:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For Each s As String In Request.Form.AllKeys
        Response.Write(s & ": " & Request.Form(s) & "<br />")
    Next
End Sub

Separate HTML page:

单独的HTML页面:

<form action="http://localhost/MyTestApp/Default.aspx" method="post">
    <input name="TextBox1" type="text" value="" id="TextBox1" />
    <input name="TextBox2" type="password" id="TextBox2" />
    <input type="submit" name="Button1" value="Button" id="Button1" />
</form>

...and it regurgitates the form values as expected. If this isn't working, as others suggested, use a traffic analysis tool (fiddler, ethereal), because something probably isn't going where you're expecting.

......并按预期反刍形式值。如果这不起作用,正如其他人建议的那样,使用流量分析工具(fiddler,ethereal),因为某些东西可能不会出现在您期望的位置。

#2


18  

The Request.Form.Keys collection will be empty if none of your html inputs have NAMEs. It's easy to forget to put them there after you've been doing .NET for a while. Just name them and you'll be good to go.

如果您的html输入都没有NAME,则Request.Form.Keys集合将为空。在你做了一段时间的.NET之后,很容易忘记将它们放在那里。只需命名它们就可以了。

#3


2  

Are you sure your HTML form is correct, and does, in fact, do an HTTP POST? I would suggest running Fiddler2, and then trying to log in via your Login.aspx, then the remote HTML site, and then comparing the requests that are sent to the server. For me, ASP.Net always worked fine -- if HTTP request contains a valid POST, I can get to values using Request.Form...

您确定您的HTML表单是否正确,并且实际上是否进行了HTTP POST?我建议运行Fiddler2,然后尝试通过您的Login.aspx,然后远程HTML站点登录,然后比较发送到服务器的请求。对我来说,ASP.Net总是工作正常 - 如果HTTP请求包含有效的POST,我可以使用Request.Form获取值...

#4


1  

You sure can.

你确定可以。

The easiest way to see how you might do this is to browse to the aspx page you want to post to. Then save the source of that page as HTML. Change the action of the form on your new html page to point back to the aspx page you originally copied it from.

了解如何执行此操作的最简单方法是浏览到要发布到的aspx页面。然后将该页面的源保存为HTML。更改新html页面上表单的操作,以指回最初复制它的aspx页面。

Add value tags to your form fields and put the data you want in there, then open the page and hit the submit button.

将值标签添加到表单字段并将所需数据放入其中,然后打开页面并单击“提交”按钮。

#5


1  

You sure can. Create an HTML page with the form in it that will contain the necessary components from the login.aspx page (i.e. username, etc), and make sure they have the same IDs. For you action, make sure it's a post.

你确定可以。创建一个包含表单的HTML页面,其中包含login.aspx页面中的必要组件(即用户名等),并确保它们具有相同的ID。为了你的行动,请确保它是一个帖子。

You might have to do some code on the login.aspx page in the Page_Load function to read the form (in the Request.Form object) and call the appropriate functions to log the user in, but other than that, you should have access to the form, and can do what you want with it.

您可能必须在Page_Load函数的login.aspx页面上执行一些代码来读取表单(在Request.Form对象中)并调用相应的函数来记录用户,但除此之外,您应该有权访问形式,并可以做你想要的。

#6


1  

In the html form, you need to supply additional viewstate variable and disable ViewState in a server page. This requires some control on both sides , though.

在html表单中,您需要提供额外的viewstate变量并在服务器页面中禁用ViewState。但这需要对双方进行一些控制。

Form HTML:

表单HTML:

<html><body> <form id='postForm' action='WebForm.aspx' method='POST'>
        <input type='text' name='postData' value='base-64-encoded-value' />
        <input type='hidden' name='__VIEWSTATE' value='' />  <!-- still need __VIEWSTATE, even empty one -->
    </form>

</body></html>

Note empty __VIEWSTATE.

注意空__VIEWSTATE。

WebForm.aspx:

WebForm.aspx:

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm.aspx.cs" Inherits="WebForm"
 EnableEventValidation="False" EnableViewState="false" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="postForm" runat="server">
        <asp:TextBox ID="postData" runat="server"></asp:TextBox>
    <div>

    </div>
    </form>
</body>
</html>

Note EnableEventValidation="False", EnableViewState="false" to prevent validation error for empty view state. Code Behind/Inherits values are not precise.

注意EnableEventValidation =“False”,EnableViewState =“false”以防止空视图状态的验证错误。 Code Behind / Inherits值不准确。

WebForm.cs:

WebForm.cs:

public partial class WebForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string value = Encoding.Unicode.GetString(Convert.FromBase64String(this.postData.Text));
    }
}

#7


0  

Hope this will help - Put this tag in html and

希望这会有所帮助 - 把这个标签放在html和

remove your login.aspx design content..just write only page directive

删除你的login.aspx设计内容..只写页面指令

and you will get the values in aspx page after submit button click like this- protected void Page_Load(object sender, EventArgs e) {

并且您将在提交按钮单击后获取aspx页面中的值,如此受保护的void Page_Load(object sender,EventArgs e){

        if (!IsPostBack)
        {
            CompleteRegistration();
        }
    }

public void CompleteRegistration() {

public void CompleteRegistration(){

        NameValueCollection nv = Request.Form;
        if (nv.Count != 0)
        {
            string strname = nv["txtbox1"];
            string strPwd = nv["txtbox2"];
        }
    }