POST 从 winform 到 aspx webform 的值

POST values from winform to aspx webform

我知道这个问题已经回答了很多次,但我仍然无法将值从 winform 发布到 aspx 页面。我总是得到一个空值。

以下是我的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 中它总是失败。

任何帮助或指导都会很棒。

提前致谢!!!..

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

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

以防万一上述 URL 停止工作:

修改 Friendly URLs 的配置 您可以在 App_Start 文件夹的 RouteConfig.cs 文件中找到它。要更改的配置是自动重定向模式,默认设置为 RedirectMode.Permanent。评论此设置:

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