如何处理 C# .NET GET/POST? [英] How to handle C# .NET GET / POST?

查看:19
本文介绍了如何处理 C# .NET GET/POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我是从 PHP 转过来后才接触 .NET 的,所以我选择了 C# 来工作,而且它的发展很好.我有一个关于 GET 和 POST 处理的问题.

As I'm new to .NET after coming from PHP I chose C# to work with and its coming along nicely. I have a question though regarding the handling of GET and POST.

到目前为止,我已经确定可以将其放在 aspx 页面后面的代码文件中:

So far I've established that I can put this in the codefile behind the aspx page:

if (Request.HttpMethod.ToString() == "POST") {

    Response.Write("You sent a post!")

}

我可以和一个 ELSE 来处理 GET,但是你会怎么做呢?

and I could and an ELSE there to handle a GET, but how exactly would you do that?

在 PHP 中我会做这样的事情:

In PHP I would do something like this:

示例网址 = http://www.example.com/page.php?foo=bar

$foobar = $_GET['foo'];

请有好心人指点我如何在 C# 中处理这个问题.

Could some kind soul please give me pointers on dealing with this in C#.

谢谢

推荐答案

$_GET[] 的 .Net 版本是:

The .Net version of $_GET[] is :

 Request.QueryString["parameter1"]

您不需要执行此 IF 条件.

You do not require to do this IF condition.

$_POST[] 的 .Net 版本是:

The .Net version of $_POST[] is :

 Request.Form["paramName"];

仍然不需要IF条件.

但是在 Asp.Net webform 中,您不需要一直使用 Request 类,因为回发到页面会将您的表单数据直接包含到控件值中.假设您有一个名为 txt1 的文本框,当用户提交表单时,您可以通过访问 txt1 直接获取该文本框的值.

BUT in Asp.Net webform you do not require to use all the time Request class because the PostBack to the page will contain your form data directly into the control value. Let say you have a textbox called txt1, when the user will submit the form you can get the value of this textbox directly by accessing txt1.

这篇关于如何处理 C# .NET GET/POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆