【问题标题】:Pass parameter to a page将参数传递给页面
【发布时间】:2012-06-19 17:29:55
【问题描述】:

我擅长 C#,但我还没有使用过 ASP.NET。我想将参数传递给页面,页面会将其打印给用户。我在我的应用程序中执行以下操作以传递 POST 类型的参数

WebRequest request = WebRequest.Create("http://www.website.com/page.aspx");
request.Method = "POST";
string post_data = "id=123&base=data";
byte[] array = Encoding.UTF8.GetBytes(post_data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = array.Length;

现在我已经传递了参数,如何从我的页面访问它们? 另外,我的上述方法对于 asp.net 发布是否正确?我用 PHP 试过了,效果很好。

【问题讨论】:

    标签: c# asp.net .net query-string


    【解决方案1】:

    在您的 aspx 页面的代码隐藏中,只需编写

    string id = Request.Form["id"].ToString();
    

    如果是发布的数据,并且

    string id = Request.Querystring["id"].ToString();
    

    如果数据在 URL 中

    【讨论】:

      【解决方案2】:

      发布数据:

      var request = (HttpWebRequest) WebRequest.Create("http://www.website.com/page.aspx");
      request.Method = "POST";
      request.ContentType = "application/x-www-form-urlencoded";
      
      var postData = Encoding.UTF8.GetBytes("id=123&base=data");
      request.ContentLength = postData.Length;
      
      using (var requestStream = request.GetRequestStream())
      {
          requestStream.Write(postData, 0, postData.Length);
      }
      

      要读取 ASP.NET 项目上发布的数据:

      var id = Int32.Parse(Request.Form["id"]);
      var data = Request.Form["base"];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        • 2012-09-16
        • 2012-01-04
        相关资源
        最近更新 更多