C# 查询字符串检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1141713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QueryString checking
提问by MAC
How to check if the web page contains any string queries at the page load?
如何在页面加载时检查网页是否包含任何字符串查询?
采纳答案by ahsteele
You can determine if there are any values in the QueryString by checking its count:
您可以通过检查其计数来确定 QueryString 中是否有任何值:
Request.QueryString.Count > 0;
That said if you are trying to prevent a page from erroring because you don't want to access a value that is not there I recommend wrapping query parms up in page properties and returning safe values from the property.
也就是说,如果您因为不想访问不存在的值而试图防止页面出错,我建议将查询参数包装在页面属性中并从该属性返回安全值。
As an example
举个例子
// setting this as protected makes it available in markup
protected string TaskName
{
get { return (string)Request.QueryString["VarName"] ?? String.Empty; }
}
回答by rahul
Check for
检查
Request.QueryString["QueryStringName"]
if you know the particular name and it returns null if there isn't any querystring by that name
如果您知道特定名称并且如果该名称没有任何查询字符串则返回 null
or if you want to check the count of querystrings then
或者如果你想检查查询字符串的数量然后
Request.QueryString.Count
and check against 0. If greater than 0 then there is atleast 1 string appended.
并检查 0。如果大于 0,则至少附加 1 个字符串。
回答by CMS
To check if the page was accessed with anyquery string, you can check the Count property:
要检查页面是否被任何查询字符串访问,您可以检查 Count 属性:
bool expression = Request.QueryString.Count > 0;
To access a defined query string parameter, you can do it like this:
要访问定义的查询字符串参数,您可以这样做:
string myParam = Request.QueryString["MyParam"];
myParam will be null if it is not on the URL.
如果 myParam 不在 URL 上,它将为 null。
回答by Muddassir Irahad
if(Request.QueryString.Count > 0)
{
//Code here
}
else
{
//Code here
}