简体   繁体   English

如何将表单从 HTML POST 到 ASPX 页面

[英]How to POST a FORM from HTML to ASPX page

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.我目前有一个使用 Membership 提供程序的 ASP.NET 站点,并且一切正常。 Users can log in from the Login.aspx page.用户可以从 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.我们现在希望能够让用户直接从另一个网站登录——这基本上是一个静态的 HTML 页面。 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 页面上输入他们的姓名和密码,并将其 POST 到我的 Login.aspx 页面(然后我可以在其中手动登录)。

Is it possible to pass form values from HTML to ASPX?是否可以将表单值从 HTML 传递到 ASPX? I have tried everything and the Request.Form.Keys collection is always empty.我已经尝试了所有方法,但 Request.Form.Keys 集合始终为空。 I can't use a HTTP GET as these are credentials and can't be passed on a query string.我不能使用 HTTP GET,因为这些是凭据,不能通过查询字符串传递。

The only way I know of is an iframe.我所知道的唯一方法是 iframe。

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),因为某些事情可能不会发生在您期望的地方。

The Request.Form.Keys collection will be empty if none of your html inputs have NAMEs.如果您的 html 输入都没有名称,则 Request.Form.Keys 集合将为空。 It's easy to forget to put them there after you've been doing .NET for a while.在您使用 .NET 一段时间后,很容易忘记将它们放在那里。 Just name them and you'll be good to go.只需为它们命名,您就可以开始了。

Are you sure your HTML form is correct, and does, in fact, do an HTTP POST?您确定您的 HTML 表单是正确的,并且确实执行了 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.我建议运行Fiddler2 ,然后尝试通过 Login.aspx 登录,然后是远程 HTML 站点,然后比较发送到服务器的请求。 For me, ASP.Net always worked fine -- if HTTP request contains a valid POST, I can get to values using Request.Form...对我来说,ASP.Net 总是工作正常——如果 HTTP 请求包含有效的 POST,我可以使用 Request.Form 获取值...

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.查看如何执行此操作的最简单方法是浏览到要发布到的 aspx 页面。 Then save the source of that page as HTML.然后将该页面的源代码另存为 HTML。 Change the action of the form on your new html page to point back to the aspx page you originally copied it from.将表单在新 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.将值标签添加到您的表单字段并将您想要的数据放入其中,然后打开页面并点击提交按钮。

You sure can.你肯定可以。 Create an HTML page with the form in it that will contain the necessary components from the login.aspx page (ie username, etc), and make sure they have the same IDs.创建一个 HTML 页面,其中包含来自 login.aspx 页面的必要组件(即用户名等),并确保它们具有相同的 ID。 For you action, make sure it's a post.对于您的操作,请确保它是一个帖子。

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 对象中)并调用适当的函数来登录用户,但除此之外,您应该有权访问的形式,并可以用它做你想做的事。

In the html form, you need to supply additional viewstate variable and disable ViewState in a server page.在 html 表单中,您需要提供额外的 viewstate 变量并在服务器页面中禁用 ViewState。 This requires some control on both sides , though.不过,这需要双方进行一些控制。

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.注意EnableEventValidation="False", EnableViewState="false"以防止空视图状态的验证错误。 Code Behind/Inherits values are not precise.代码隐藏/继承值不精确。

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));
    }
}

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 页面中的值,如下所示 - protected void Page_Load(object sender, EventArgs e) {

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

public void CompleteRegistration() {公共无效完成注册(){

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

删除数据发布/发布 .aspx 页面的 runat="server" 部分。

I found a working solution in https://www.mikesdotnetting.com/article/293/request-form-is-empty-when-posting-to-aspx-page .我在https://www.mikesdotnetting.com/article/293/request-form-is-empty-when-posting-to-aspx-page找到了一个有效的解决方案。 The key is, remove .aspx from action attribute.关键是,从 action 属性中删除 .aspx。

EX: <FORM NAME="Logon" action="default" method="post">例如: <FORM NAME="Logon" action="default" method="post">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM