【问题标题】:Request.QueryString not recognized, System.Web included in namespaceRequest.QueryString 无法识别,System.Web 包含在命名空间中
【发布时间】:2013-10-01 18:37:32
【问题描述】:

一个简单的问题,但我宁愿问,因为我有两次问题。

我正在尝试从 ASP.NET 项目中的 url 检索主机名。代码实际上不在网页中,而是在一个类(域的一部分)中。 System.Web 包含在标题中。当我尝试使用 Request.QueryString 时,它无法识别。更糟糕的是,如果我尝试 HttpContext.Current,我会收到此错误

'System.Web.HttpContext.Current' is null

这是我的代码

using System.Web;

public class MyNightlyJob : AbstractJob
{
    public override void ExecuteJob(IJobExecutionContext context)
    {
          HttpContext.Current.Request.ServerVariables["HTTP_HOST"]; // does not work
          Request.ServerVariables["HTTP_HOST"]; // this does not work also
    }
}

我错过了什么?请注意,我的问题实际上是关于 Request.ServerVariables 但如果一个有效,另一个也将有效。

请注意,代码中根本无法识别 Request.ServerVariable。 HttpContext.Current 被识别,但我得到了运行时错误。

【问题讨论】:

  • 你从哪里调用这个方法?它是否与 HTTP 请求在同一线程上执行?
  • 完全不确定该线程。如果有帮助,我在一个解决方案中有 4 个应用程序。它是 Web 应用程序,而不是线程应用程序 AFAIK。
  • 试试 HttpContext.Current.Request.QueryString["HTTP_HOST"]
  • 也许您可以发布调用此方法的代码?听起来您没有在正确的范围内使用它。
  • 我可以逐步完成。我猜 ExecuteJob 会自动调用。它是 Quartz.NET 的一部分。

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


【解决方案1】:

您需要检查 HttpContext.Current 是否为空。如果它为 null,则它不在要检索的正确上下文中。

如果您可以修改函数,我会将 HttpContext.Current 作为变量传递或将主机作为变量传递。

【讨论】:

  • 这是一个好主意,但它是一个大代码,我自己不想做这个改变。是的,HttpContext.Current 在我的代码中返回 null。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-21
  • 2011-10-05
  • 2012-05-07
  • 2019-12-19
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
相关资源
最近更新 更多