Siddharth
Siddharth

Reputation: 456

POST values from winform to aspx webform

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!!!..

Upvotes: 0

Views: 182

Answers (1)

Jamal
Jamal

Reputation: 422

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

Upvotes: 1

Related Questions