从Winform到ASPX WebForm的POST值

时间:2019-07-15 07:09:53

标签: c# asp.net http-post winforms-to-web

我知道这个问题已经回答了很多次,但是在将值从Winform发布到ASPX页面时,我仍然遇到问题。我总是得到null值。

以下是我的winform代码:

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

以下是我的Default.aspx页面代码:

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"];
    }

“请求表单”值始终为空值。相同的代码在MVC中有效,但在Webforms中始终会失败。

任何帮助或指导都是很好的。

在此先感谢!!! ..

1 个答案:

答案 0 :(得分:1)

如果您在网站上启用了FriendlyURLS,它将干扰Request.Form的收集:

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

以防上述URL停止工作:

修改友好URL的配置您将在App_Start文件夹的RouteConfig.cs文件中找到它。要更改的配置是自动重定向模式,默认情况下设置为RedirectMode.Permanent。注释掉此设置:

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