如何在C#中获取实际的url字符串路径 [英] How to get actual url string path in C#
本文介绍了如何在C#中获取实际的url字符串路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面提到了我想要的。 Ex.2很紧急:
Ex.1
输入字符串:horse.shop.com
输出网址:http://horse.shop.com/
Ex.2
输入字符串:shop.com/horse
输出网址:http://shop.com/
提前致谢。
恭敬地,
Emon Mahmud
I have mentioned below that i want. Ex.2 is urgent:
Ex.1
input string : horse.shop.com
output url : http://horse.shop.com/
Ex.2
input string : shop.com/horse
output url : http://shop.com/
Thanks in advance.
Respectfully,
Emon Mahmud
推荐答案
使用正则表达式格式检查网址,如果它无效然后在字符串的开头添加http或https ...你会得到它的路径
check url with regex format and if it is not valid then add http or https at started of string ... u will get path of it
如果真实的URL:http://shop.com/horse
url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://shop.com/horse
string path = HttpContext.Current.Request.Url.AbsolutePath;
// / horse
string host = HttpContext .Current.Request.Url.Host;
// shop.com
if Real URL: http://shop.com/horse
url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://shop.com/horse
string path = HttpContext.Current.Request.Url.AbsolutePath;
// /horse
string host = HttpContext.Current.Request.Url.Host;
// shop.com
这篇关于如何在C#中获取实际的url字符串路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文