Hi
I can’t get wwwform to work with asp.net form on a site
The form works great as I expect directly from the website, but when I create a wwwform object inside unity and add a field for the text box in the site , it behave like the wwwform object doesn’t do anything and always return the web page without submitting the data
The code in the unity is fine and works great with php forms
I don’t know where the problem is, I tried adding code in code behind file in different page cycle events but still nothing get submitted to the form
I made sure that the form method is POST as well (that is the behavior I need)
Is there something specific I need to do to make wwwform interact with asp.net page that I’m not aware off ?
like the code must be on a specific page cycle event or some properties that must be set for the form or the control I’m submitting to ?
Please point me out to the right direction to get it to work like it do with php
I’d be very grateful if you post an example
Thanks in advance
Please share your knowledge if you have got it to work
I’ve been using asp.net generic http handlers with Unity successfully.
public class MyHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string code = context.Request.Form["code"];
// process request
context.Response.ContentType = "text/plain"; // or xml, json... whatever you need
context.Response.Write(result_string);
}
public bool IsReusable { get { return false; } } // or true, whatever your need
}
IEnumerator coWWW(string code)
{
WWWForm wwwForm = new WWWForm();
wwwForm.AddField("code", code);
WWW www = new WWW("http://mydomain.com/MyHandler.ashx", wwwForm);
while (!www.isDone string.IsNullOrEmpty(www.error)) yield return null;
if (!string.IsNullOrEmpty(www.error))
Debug.Log("WWW Error: " + www.error);
else
Debug.Log("WWW Result = " + www.text);
}
I have also used asp.net Page which also implements IHttpHandler. You should be fine with handling your post data in Load or PreRender event.
Can you share your backend code?
Thanks very much nikolic for trying to help
I know nothing about IHttpHandler, and I have no clue what the advantage of using it ?
Where should I add this class ? in asp.net code behind you mean ?
I guess you mean in code behind
I see no point using this class , the text field do the same I guess ?
if I rename the text field to “Field” for example I can just use Field.Text, or is context.Request.Form[“code”] is different ?
anyway I tried using it this way according to what I understood but it doesn’t work, may be if you explain a little bit more ?
as for my code
I use this code in Unity
IEnumerator GetValues()
{
WWWForm form = new WWWForm();
for (int i = 0; i < formFields.Length; i++) //formFields is an array of strings that I populate with the fields I need to add
{
form.AddField("Field" + i.ToString(), formFields[i]);
}
WWW postUrl = new WWW(url, form); // url is a string that holds the web page path
yield return postUrl;
if (postUrl.error != null)
{
// processing error here
}
else
{
Debug.Log(postUrl.text);
// do my logic here
}
With PHP I use the following code to work with the submitted fields, and it works great without any problems
<?php
if(isset($_POST['Field0']) isset($_POST['Field1']))
{
$fieldNames = array();
for($i = 0; $i < 2; $i++)
{
$fieldNames[$i] = $_POST['Field' . $i];
}
// do my logic here
exit();
}
?>
<form action='pageName.php' method=POST>
<?php
for($i = 0; $i < 2; $i++)
{
?>
<input type=hidden name=Field<?php echo $i ?> >
<?php
}
?>
</form>
But on asp.net page nothing works
here is the aspx file content
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="test3.aspx.cs" Inherits="test3" %>
<form id="form1" runat="server" method="post" >
<asp:TextBox ID="Field0" runat="server"></asp:TextBox>
</form>
and the code behind file
protected void Page_Load(object sender, EventArgs e)
{
if (Field0.Text != "")
{
Response.Write("Works");
Response.End();
}
}
but it always returns the text in the original aspx page and never works and acts like nothing has been submitted from Unity
I’d appreciate if you explain a little bit more as I’m still can’t get it to work and I need to switch from php to asp.net
Hi there, from what I remember, you need to check for Page.IsPostBack on your Page_Load event to detect when something has been posted, I don’t know if it will solve your problem, but may help you a little bit to organize your logic.
Since you are only posting from unity I assume if you access your Field0 on your code behind file it will try to read the value from the Viewstate, in that case you can try to get the value directly from Request.Form[“Field0”]
Thanks thempus, finally I feel that I’m not left alone
Yeah I did check Page.IsPostBack earlier but still the same
after reading your post I did exactly what you suggested and made a little test
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (Request.Form["Field0"] != "")
{
Response.Write(Request.Form["Field0"]);
Response.End();
}
else
{
Response.Write("Empty");
Response.End();
}
}
else
{
Response.Write("Not post back");
}
}
and guess what , I got last message “Not post back”
It still acts as I mentioned in the first post , it seems that unity don’t want to submit any form fields to aspx pages
I even made the text field’s AutoPostBack property true and put same code in onTextChanged event but still same result 
though it works when I test on the page itself
I’m not sure about that, but maybe IsPostBack only works for when you use webform controls, for an external request I managed to get it to work using Page.Request.HttpMethod == “POST” instead of Page.IsPostBack, also the field value is returning fine
aspx:
%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.Request.HttpMethod == "POST")
{
Response.Write("Is Post: ");
Response.Write(Request.Form["Field0"]);
Response.End();
}
else
{
Response.Write("Not Post");
Response.End();
}
}
unity
IEnumerator Start () {
WWWForm form = new WWWForm();
form.AddField("Field0" , "Value Test");
var r = new WWW(url, form);
yield return r;
if (r.error != null)
{
}
else
{
Debug.Log("Success");
Debug.Log(r.text);
}
}
1 Like
Woooot it worked this way
You are awesome thempus, thanks very much