2015-09-06 31 views
1

令我遗憾的是,因为我几乎不能使用C#,所以无法从URL中读取参数。无法获取Request.QueryString或类似的方法来工作

我在我的IIS 7上运行一个C#cgi可执行文件作为应用程序。调用可执行文件的URL是如下:

https://server/cgi/showEmail/[email protected]

的代码如下开始:

using System; 
using System.Web; // <---- isn't this for Request.QueryString ? 
using System.Web.UI; 
using System.Collections; 
using System.Collections.Specialized; 
using System.Data.SqlClient; 

class showEmail 
{ 
    static void Main(string[] args) 
    { 

     Console.WriteLine("\r\n\r\n"); 
     Console.WriteLine("<h1>Test</h1>"); 

     try 
     { 

现在,如果我用下面的代码,程序编译,但当在浏览器中执行时,会给出这个空例外:

 string email = HttpContext.Current.Request.QueryString["email"]; 

System.NullReferenceException:未将对象引用设置为对象的实例。在showEmail.Main(字串[] args)

,如果我用下面这段代码,乐趣的编译器,谁给了 “当前上下文” 异常已停止:

 string email = Request.QueryString["email"]; 

错误CS0103:名称“请求”不会在目前情况下

存在...

我失去的东西这是可执行文件所需的基本参数吗?

编辑:我已经通过sof和许多其他地方看过,但到目前为止还没有能够在这个问题上连接点。

+0

我在同和你一起玩 - 我对C#Razor很陌生,似乎无法弄清楚为什么Request.QueryString不起作用。我一直在搜索超过两个小时,似乎无法找到需要什么集合,以及可用于评估查询字符串属性的各种方法。我见过的所有东西都不起作用,因为这个原因在当前的环境中不存在。 –

回答

0

HttpContext.Current.Request在控制台应用程序中不可用。

使用args参数来接收查询字符串参数,就像我在下面做的那样。

for (int i = 0; i < args.Length; i++) 
{ 
    Console.WriteLine("parameter[{0}] is [{1}]", i, args[i]); 
} 

您可能需要使用以下代码才能从args中收到的url中提取参数。

var url=args[0]; 

var queryString = url.Substring(url.IndexOf('?')).Split('#')[0] 
System.Web.HttpUtility.ParseQueryString(queryString) 
+0

感谢您指点我正确的方向。不幸的是,使用你的建议,参数的长度是0,所以无论是我的IIS在查询字符串到达​​应用程序之前过滤,还是还有一些其他问题仍然在我的代码中发现。 –

0

这是我得到了它的最后工作,请记住这是不专业的验证代码,更多的尝试和努力尝试...

using System; 
using System.Data; 
using System.Configuration; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     string email = Request.QueryString["email"] ?? "null"; 
     if (email != "null") {