0

I know this question has been answered a lot of times, but I am still having issues with posting values from winform to aspx page. I always get a value of null.

Following is my winform code:

string formContent = "FormValue1=" + contact.Country;

        var dataBytes = System.Text.Encoding.UTF8.GetBytes(formContent);
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:52262/Default.aspx");
        httpWebRequest.ContentType = "application/x-www-form-urlencoded";           

        httpWebRequest.ContentLength = dataBytes.Length;

        httpWebRequest.Method = WebRequestMethods.Http.Post;

        Stream dataStream = httpWebRequest.GetRequestStream();            

        dataStream.Write(dataBytes, 0, dataBytes.Length);            
        dataStream.Close();
        //dataStream.Flush();

        HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();

        //lblShow.Text = ((HttpWebResponse)response).StatusDescription;

        Stream responseStream = response.GetResponseStream();

        StreamReader streamReader = new StreamReader(responseStream);

        string responseFromServer = streamReader.ReadToEnd();          

        streamReader.Close();                        
        response.Close();   

Following is my Default.aspx page code:

protected void Page_Load(object sender, EventArgs e)
    {
        ClaimsPrincipal claimsPrincipal = Page.User as ClaimsPrincipal;

        if (claimsPrincipal != null)
        {
            this.ClaimsGridView.DataSource = claimsPrincipal.Claims;
            this.ClaimsGridView.DataBind();
        }

        Session["Name"] = Request.Form["FormValue1"];
    }

The Request Form values always have null values. The same code works in MVC, but in webforms it always fails.

Any help or guidance would be great.

Thanks In Advance!!!..

1 Answer 1

1

If you have FriendlyURLS enabled on the website, it will interfere with Request.Form collection:

Checkout: https://www.mikesdotnetting.com/article/293/request-form-is-empty-when-posting-to-aspx-page

Just in case the above URL stops working:

Amend the configuration for Friendly URLs You will find this in the RouteConfig.cs file in the App_Start folder. The configuration to change is the AutoRedirect mode which by default is set to RedirectMode.Permanent. Comment this setting out:

public static void RegisterRoutes(RouteCollection routes)
{
    var settings = new FriendlyUrlSettings();
    //settings.AutoRedirectMode = RedirectMode.Permanent;
    routes.EnableFriendlyUrls(settings);
}

Sign up to request clarification or add additional context in comments.

3 Comments

I can now get Form values. Thanks for this approach.
Also how can we get Session values which was already set at the website level, before calling winform app. So when posting values back from winform to website, I can re-use the Session variables.
Session values are stored in memory on the Web server, by default. You need to store session values in a SQL database, an ASP.NET state server, or a custom server. This preserves session values if the ASP.NET or IIS process or the ASP.NET application restarts to make session values available across all the servers in a Web farm. This behavior is configured by setting the mode attribute to a valid SessionStateMode value in the sessionState element of your application configuration. See docs.microsoft.com/en-us/dotnet/api/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.