简体   繁体   English

如何从任何地方访问 Request.QueryString?

[英]Howto access Request.QueryString from anywhere?

I've gotten an error with the Request.QueryString and I cant seem to find any solution..我遇到了 Request.QueryString 错误,我似乎找不到任何解决方案。

 public static DataTable SelectFrom(string Table, string Felt, string query)
  {
    DataTable dt = new DataTable();
    try
    {

        SqlCommand cmd = new SqlCommand("SELECT * FROM " + Table + " WHERE " + Felt + " = @parameter", conn);
        cmd.Parameters.AddWithValue("@parameter", Request.QueryString[query]);

        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        ad.Fill(dt);
    }
    catch
    {

    }
    return dt;
}

似乎您的代码在单独的类或程序集中(不在页面类中),如果是,请使用

httpcontext.current.request.QueryString

You are trying to get QueryString inside static method and there is no Request object inside this method so you need to use.您正在尝试在static方法中获取QueryString并且此方法中没有Request object因此您需要使用。

HttpContext.Current.Request.QueryString 

in static method.在静态方法中。

Let us know the error that you got.让我们知道您遇到的错误。

Always before using Request.QueryString you need to check it for null, like总是在使用 Request.QueryString 之前,您需要检查它是否为空,例如

if(Request.QueryString[query] != null && Request.QueryString[query].ToString() != string.empty)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM