如何验证字符串是否有效Aspx Url? [英] How to verify a string is valid Aspx Url or not?
问题描述
大家好,
我是初级开发人员,所以我还没有选择正则表达式(> o<)。
在目前的情况下,我想检查SharePoint URL是否有效。
在验证sharepoint url之前,我想验证它是否是正确的aspx url ...
例如:
一一
我的简单编码如下:
In current situation, I want to check SharePoint URL is valid or not.
Before verify the sharepoint url, I want to verify it is correct aspx url...
For example:
http://localhost:8181/Test_ASPX_To_HTML/test0.aspx (Valid)
http://localhost/Test_ASPX_To_HTML/test1.aspx (Valid)
https://www.helloworld.com/Test_ASPX_To_HTML/test2.aspx (Valid)
https://www.helloworld.co.jp/Test_ASPX_To_HTML/test3.aspx (Valid)
http://www.helloworld.org.hk/Test_ASPX_To_HTML/test4.aspx (Valid)
https://www.helloworld.tv:1233/Test_ASPX_To_HTML/test5.aspx (Valid)
https://localhost:8181/Test_ASPX_To_HTML/test6.aspxx (Invalid)
https://localhost:8181/Test_ASPX_To_HTML/test7.asp (Invalid)
https://localhost:8181/Test_ASPX_To_HTML/test8.asax (Invalid)
https://localhost:8181\Test_ASPX_To_HTML\test9.asax (Invalid)
http://123@123.aspx (Invalid)
http://~!@#$%^^&*(((((((((((((.aspx (Invalid)
http://localhost:8181/Test_ASPX_To_HTML/test10.aspx?a=1 (Valid)
http://localhost:8181/Test_ASPX_To_HTML/test11.aspx?a=2&b=3 (Valid)
http://www.test.net/Test_ASPX_To_HTML/test12.aspx?a=2&b=3&c=4 (Valid)
http://www.test.net\Test_ASPX_To_HTML\test13.aspx?a=2&b=3&c=4 (Invalid)
www.test.net/Test_ASPX_To_HTML/test14.aspx?a=2&b=3&c=4 (Invalid)
ftp://www.test.net/Test_ASPX_To_HTML/test15.aspx (Invalid)
http://www.test.net\Test_ASPX_To_HTML\test16.aspx?a=2&b=3&c=4 (Invalid)
And it must not have any invalid char. etc.
My simple coding as follows:
if (strTargetASPX.Substring(0 ,4)。ToLower()。Equals(" http" )&&
if (strTargetASPX.Substring(0, 4).ToLower().Equals("http") &&
strTargetASPX.Substring(strTargetASPX .Length - 4,4).ToLower()。Equals(" aspx" )&& // Wrong
strTargetASPX.Substring(strTargetASPX.Length - 4, 4).ToLower().Equals("aspx") && //Wrong
strTargetASPX.IndexOf("!" )< 0&&
strTargetASPX.IndexOf("!") < 0 &&
strTargetASPX.IndexOf(" @" ; )< 0&&
strTargetASPX.IndexOf("@") < 0 &&
strTargetASPX.IndexOf("#" )< 0&&
strTargetASPX.IndexOf("#") < 0 &&
strTargetASPX.IndexOf(" $" )< 0&&
strTargetASPX.IndexOf("$") < 0 &&
strTargetASPX.IndexOf(" ^" )< 0&&
strTargetASPX.IndexOf("^") < 0 &&
strTargetASPX.IndexOf(" *" )< 0&&
strTargetASPX.IndexOf("*") < 0 &&
strTargetASPX.IndexOf("(&)< 0&&
strTargetASPX.IndexOf("(") < 0 &&
strTargetASPX.IndexOf(")" )< 0&&
strTargetASPX.IndexOf(")") < 0 &&
strTargetASPX.IndexOf(" - " )< 0&&
strTargetASPX.IndexOf("-") < 0 &&
strTargetASPX.IndexOf(" +" )< 0&&
strTargetASPX.IndexOf("+") < 0 &&
strTargetASPX.IndexOf( @" \" )< 0&&
strTargetASPX.IndexOf(@"\") < 0 &&
strTargetASPX.IndexOf(" \"" )< 0&&
strTargetASPX.IndexOf("\"") < 0 &&
strTargetASPX.IndexOf(" ;;" )< 0&&
strTargetASPX.IndexOf(";") < 0 &&
strTargetASPX.IndexOf(" |" )< 0&&
strTargetASPX.IndexOf("|") < 0 &&
strTargetASPX.IndexOf(" [" )< 0&&
strTargetASPX.IndexOf("[") < 0 &&
strTargetASPX.IndexOf("]" )< 0&&
strTargetASPX.IndexOf("]") < 0 &&
strTargetASPX.IndexOf(" {" )< 0&&
strTargetASPX.IndexOf("{") < 0 &&
strTargetASPX.IndexOf("}" )< 0&&
strTargetASPX.IndexOf("}") < 0 &&
strTargetASPX.IndexOf("`" )< 0
strTargetASPX.IndexOf("`") < 0
)
{
//做点什么
//Do something
}
我不确定上面的 检查是否完全正确(至少不能通过子串检查aspx,它与aspx不匹配?a = 1& b = 2等。)因此,我想通过正则表达式获得有关最佳实践的建议。
你能帮帮我吗?
And I’m not sure the above checking is exactly correct or not (at least cannot check aspx by using Substring, it cannot match the aspx?a=1&b=2 etc.), therefore, I would like to get your advice for best practices with regular expressions.
Could you please help me?
推荐答案
查看我的博客
http://lamahashim.blogspot.com/2009/06/validating-url-using-c。 html
Check my blog
http://lamahashim.blogspot.com/2009/06/validating-url-using-c.html
这篇关于如何验证字符串是否有效Aspx Url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!