获得Url的地址属性
2014-10-24 10:34:47 访问(2141) 赞(0) 踩(0)
/// <summary>
/// 以 http://localhost:5160/WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx?id=1 为例
/// </summary>
public enum EmWebInfo
{
/// <summary>
/// IP地址
/// 127.0.0.1
/// </summary>
IP地址 = 1,
/// <summary>
/// 是否本地
/// </summary>
是否本地,
/// <summary>
/// /WebSite
/// </summary>
PhyPath,
/// <summary>
/// C:\Projectes\SlowX\SRC\Code\SlowXWebSite\WebSite\
/// </summary>
物理根路径,
/// <summary>
/// http://localhost:5160/WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx?id=1
/// </summary>
完整URL地址,
/// <summary>
/// http://localhost:5160/WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx
/// </summary>
无参数完整URL地址,
/// <summary>
/// http://www.baidu.com
/// </summary>
上次URL地址,
/// <summary>
/// C:\Projectes\SlowX\SRC\Code\SlowXWebSite\WebSite\WebForms\WebPages\code\dotnet\HttpContextInfo\Info\Default.aspx
/// </summary>
页面的完整物理地址,
/// <summary>
/// /WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx
/// </summary>
虚拟目录加页面地址,
/// <summary>
/// /WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx?id=1
/// </summary>
虚拟目录加完整参数页面地址,
/// <summary>
/// ?id=1
/// </summary>
参数内容_带问号,
/// <summary>
/// id=1
/// </summary>
参数内容_不带问号,
/// <summary>
/// http://localhost:5160
/// </summary>
网页根目录虚拟地址,
/// <summary>
/// http://localhost:5160/WebSite
/// </summary>
网页根目录虚拟地址_带虚拟目录,
/// <summary>
/// /WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx
/// </summary>
当前页面所在根目录地址,
/// <summary>
/// /WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx?id=1
/// </summary>
当前页面所在根目录地址_带参数,
/// <summary>
/// Default.aspx
/// </summary>
页面名称,
/// <summary>
/// Default
/// </summary>
页面名称_过滤后缀,
/// <summary>
/// Default.aspx?id=1
/// </summary>
页面名称和参数,
/// <summary>
/// http://localhost:5160/WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info
/// </summary>
页面当前目录的虚拟完整地址,
/// <summary>
/// /WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info
/// </summary>
页面当前目录相对根的地址,
/// <summary>
/// /WebForms/WebPages/code/dotnet/HttpContextInfo/Info
/// </summary>
页面当前目录相对网站的地址,
/// <summary>
/// C:\Projectes\SlowX\SRC\Code\SlowXWebSite\WebSite\WebForms\WebPages\code\dotnet\HttpContextInfo\Info
/// </summary>
页面当前目录的物理地址,
/// <summary>
/// Info
/// </summary>
页面所在目录名
}
/// <summary>
/// 数组
/// </summary>
public readonly static EmWebInfo[] EmArray = new EmWebInfo[]
{
EmWebInfo.IP地址,
EmWebInfo.是否本地,
EmWebInfo.PhyPath,
EmWebInfo.物理根路径,
EmWebInfo.完整URL地址,
EmWebInfo.无参数完整URL地址,
EmWebInfo.上次URL地址,
EmWebInfo.页面的完整物理地址,
EmWebInfo.虚拟目录加页面地址,
EmWebInfo.虚拟目录加完整参数页面地址,
EmWebInfo.参数内容_带问号,
EmWebInfo.参数内容_不带问号,
EmWebInfo.网页根目录虚拟地址,
EmWebInfo.网页根目录虚拟地址_带虚拟目录,
EmWebInfo.当前页面所在根目录地址,
EmWebInfo.当前页面所在根目录地址_带参数,
EmWebInfo.页面名称,
EmWebInfo.页面名称_过滤后缀,
EmWebInfo.页面名称和参数,
EmWebInfo.页面当前目录的虚拟完整地址,
EmWebInfo.页面当前目录相对根的地址,
EmWebInfo.页面当前目录相对网站的地址,
EmWebInfo.页面当前目录的物理地址,
EmWebInfo.页面所在目录名
};
/// <summary>
///
/// </summary>
/// <param name="em"></param>
/// <returns></returns>
public static string GetMethodName(EmWebInfo em)
{
switch (em)
{
case EmWebInfo.IP地址:
return "GetIp";
case EmWebInfo.是否本地:
return IsLocalHost().ToString();
case EmWebInfo.PhyPath:
return "GetPhyPath";
case EmWebInfo.参数内容_不带问号:
return "GetUrlQuery";
case EmWebInfo.参数内容_带问号:
return "GetUrlQueryParams";
case EmWebInfo.当前页面所在根目录地址:
return "GetPagePath";
case EmWebInfo.当前页面所在根目录地址_带参数:
return "GetPagePathAndParams";
case EmWebInfo.上次URL地址:
return "GetUrlReferrer";
case EmWebInfo.完整URL地址:
return "GetAbsoluteUri";
case EmWebInfo.网页根目录虚拟地址:
return "GetWebDomainUrl";
case EmWebInfo.网页根目录虚拟地址_带虚拟目录:
return "GetWebRootUrl";
case EmWebInfo.无参数完整URL地址:
return "GetAbsoluteUriNoParams";
case EmWebInfo.物理根路径:
return "GetPhysicalApplicationPath";
case EmWebInfo.虚拟目录加完整参数页面地址:
return "GetPathAndQuery";
case EmWebInfo.虚拟目录加页面地址:
return "GetLocalPath";
case EmWebInfo.页面当前目录的物理地址:
return "GetCurPageDirPhysicalPath";
case EmWebInfo.页面当前目录的虚拟完整地址:
return "GetPageDirWebPath";
case EmWebInfo.页面当前目录相对根的地址:
return "GetCurDirWebRootPath";
case EmWebInfo.页面当前目录相对网站的地址:
return "GetCurDirWebSitePath";
case EmWebInfo.页面的完整物理地址:
return "GetPhysicalPath";
case EmWebInfo.页面名称:
return "GetCurPageName";
case EmWebInfo.页面名称_过滤后缀:
return "GetCurPageOnlyName";
case EmWebInfo.页面名称和参数:
return "GetCurPageNameAndQuery";
case EmWebInfo.页面所在目录名:
return "GetCurPageDirName";
default:
return "";
}
}
/// <summary>
///
/// </summary>
/// <param name="em"></param>
/// <returns></returns>
public static string GetInfo(EmWebInfo em)
{
switch (em)
{
case EmWebInfo.IP地址:
return GetIp();
case EmWebInfo.是否本地:
return IsLocalHost().ToString();
case EmWebInfo.PhyPath:
return GetPhyPath();
case EmWebInfo.参数内容_不带问号:
return GetUrlQuery();
case EmWebInfo.参数内容_带问号:
return GetUrlQueryParams();
case EmWebInfo.当前页面所在根目录地址:
return GetPagePath();
case EmWebInfo.当前页面所在根目录地址_带参数:
return GetPagePathAndParams();
case EmWebInfo.上次URL地址:
return GetUrlReferrer();
case EmWebInfo.完整URL地址:
return GetAbsoluteUri();
case EmWebInfo.网页根目录虚拟地址:
return GetWebDomainUrl();
case EmWebInfo.网页根目录虚拟地址_带虚拟目录:
return GetWebRootUrl();
case EmWebInfo.无参数完整URL地址:
return GetAbsoluteUriNoParams();
case EmWebInfo.物理根路径:
return GetPhysicalApplicationPath();
case EmWebInfo.虚拟目录加完整参数页面地址:
return GetPathAndQuery();
case EmWebInfo.虚拟目录加页面地址:
return GetLocalPath();
case EmWebInfo.页面当前目录的物理地址:
return GetCurPageDirPhysicalPath();
case EmWebInfo.页面当前目录的虚拟完整地址:
return GetPageDirWebPath();
case EmWebInfo.页面当前目录相对根的地址:
return GetCurDirWebRootPath();
case EmWebInfo.页面当前目录相对网站的地址:
return GetCurDirWebSitePath();
case EmWebInfo.页面的完整物理地址:
return GetPhysicalPath();
case EmWebInfo.页面名称:
return GetCurPageName();
case EmWebInfo.页面名称_过滤后缀:
return GetCurPageOnlyName();
case EmWebInfo.页面名称和参数:
return GetCurPageNameAndQuery();
case EmWebInfo.页面所在目录名:
return GetCurPageDirName();
default:
return "";
}
}
/// <summary>
/// 页面当前目录的虚拟完整地址
/// http://localhost:5160/WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info
/// </summary>
/// <returns></returns>
public static string GetPageDirWebPath()
{
HttpContext hc = HttpContext.Current;
string theResult = hc.Request.Url.AbsoluteUri;
if (theResult == null || theResult.Length == 0)
return "";
int idx = theResult.LastIndexOf("?");
if (idx != -1)
theResult = theResult.Substring(0, idx);
idx = theResult.LastIndexOf("/");
if (idx != -1)
theResult = theResult.Substring(0, idx);
return theResult;
}
/// <summary>
/// 页面的完整物理地址
/// C:\Projectes\SlowX\SRC\Code\SlowXWebSite\WebSite\WebForms\WebPages\code\dotnet\HttpContextInfo\Info\Default.aspx
/// </summary>
/// <returns></returns>
public static string GetPhysicalPath()
{
HttpContext hc = HttpContext.Current;
return hc.Request.PhysicalPath;
}
/// <summary>
/// 页面当前目录的物理地址
/// C:\Projectes\SlowX\SRC\Code\SlowXWebSite\WebSite\WebForms\WebPages\code\dotnet\HttpContextInfo\Info
/// </summary>
/// <returns></returns>
public static string GetCurPageDirPhysicalPath()
{
HttpContext hc = HttpContext.Current;
string str = hc.Request.PhysicalPath;
if (str == null || str.Length == 0)
return "";
FileInfo info = new FileInfo(str);
return info.Directory.FullName;
}
/// <summary>
/// 页面所在目录名
/// Info
/// </summary>
/// <returns></returns>
public static string GetCurPageDirName()
{
HttpContext hc = HttpContext.Current;
string str = hc.Request.PhysicalPath;
if (str == null || str.Length == 0)
return "";
FileInfo info = new FileInfo(str);
return info.Directory.Name;
}
/// <summary>
/// 页面当前目录相对根的地址
/// /WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info
/// </summary>
/// <returns></returns>
public static string GetCurDirWebRootPath()
{
HttpContext hc = HttpContext.Current;
string str= hc.Request.Url.PathAndQuery;
int idx = str.LastIndexOf('/');
if (idx == -1)
return "";
return str.Substring(0, idx);
}
/// <summary>
/// 页面当前目录相对网站的地址
/// /WebForms/WebPages/code/dotnet/HttpContextInfo/Info
/// </summary>
/// <returns></returns>
public static string GetCurDirWebSitePath()
{
HttpContext hc = HttpContext.Current;
string str = hc.Request.Url.PathAndQuery;
int idx = str.LastIndexOf('/');
if (idx == -1)
return "";
str = str.Substring(0, idx);
if (hc.Request.ApplicationPath == "/")
return str;
return str.Substring(hc.Request.ApplicationPath.Length);
}
/// <summary>
/// 虚拟目录加完整参数页面地址
/// /WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx?id=1
/// </summary>
/// <returns></returns>
public static string GetPathAndQuery()
{
HttpContext hc = HttpContext.Current;
return hc.Request.Url.PathAndQuery;
}
/// <summary>
/// 虚拟目录加页面地址
/// /WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx
/// </summary>
/// <returns></returns>
public static string GetLocalPath()
{
HttpContext hc = HttpContext.Current;
return hc.Request.Url.LocalPath;
}
/// <summary>
/// 物理根路径
/// C:\Projectes\SlowX\SRC\Code\SlowXWebSite\WebSite\
/// </summary>
/// <returns></returns>
public static string GetPhysicalApplicationPath()
{
HttpContext hc = HttpContext.Current;
return hc.Request.PhysicalApplicationPath;
}
/// <summary>
/// 完整URL地址
/// http://localhost:5160/WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx?id=1
/// </summary>
/// <returns></returns>
public static string GetAbsoluteUri()
{
HttpContext hc = HttpContext.Current;
return hc.Request.Url.AbsoluteUri;
}
/// <summary>
/// 无参数完整URL地址
/// http://localhost:5160/WebSite/WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx
/// </summary>
/// <returns></returns>
public static string GetAbsoluteUriNoParams()
{
HttpContext hc = HttpContext.Current;
string str = hc.Request.Url.AbsoluteUri;
int idx = str.LastIndexOf('?');
if (idx == -1)
return str;
return str.Substring(0, idx);
}
/// <summary>
/// 上次URL地址
/// </summary>
/// <returns></returns>
public static string GetUrlReferrer()
{
if (HttpContext.Current == null)
return "";
HttpRequest hr = HttpContext.Current.Request;
if (hr.UrlReferrer == null)
return "";
return hr.UrlReferrer.AbsoluteUri;
}
/// <summary>
/// 网页根目录虚拟地址_带虚拟目录
/// http://localhost:5160/WebSite
/// </summary>
/// <returns></returns>
public static string GetWebRootUrl()
{
HttpContext hc = HttpContext.Current;
// 虚拟目录加完整参数页面地址 //
// /SlowXWebSite/Test/WebCommon/Default.aspx?id=default.aspx&web=%cb%aa%d2%b6&dt=D%3a%5ccanoe%5cs.aspx&p=fdf%5cf%2ffds.fdsf%3ffdf //
string strPathAndQuery = hc.Request.Url.PathAndQuery;
// 完整URL地址 //
string strAbsoluteUri = hc.Request.Url.AbsoluteUri;
int idx = strAbsoluteUri.LastIndexOf(strPathAndQuery);
if (idx == -1)
throw ThrowSlowXFunctionsExceptions.NewSlowXFunctionsExceptionIsNullMessage
(
MethodBase.GetCurrentMethod(),
"int idx = strAbsoluteUri[" + strAbsoluteUri + "].LastIndexOf(strPathAndQuery[" + strPathAndQuery + "]) == -1;"
);
string theResult = strAbsoluteUri.Substring(0, idx);
if (hc.Request.ApplicationPath == "/")
return theResult;
else
return theResult + hc.Request.ApplicationPath;
}
/// <summary>
/// 网页根目录虚拟地址
/// http://localhost:5160
/// </summary>
/// <returns></returns>
public static string GetWebDomainUrl()
{
HttpContext hc = HttpContext.Current;
// 虚拟目录加完整参数页面地址 //
// /SlowXWebSite/Test/WebCommon/Default.aspx?id=default.aspx&web=%cb%aa%d2%b6&dt=D%3a%5ccanoe%5cs.aspx&p=fdf%5cf%2ffds.fdsf%3ffdf //
string strPathAndQuery = hc.Request.Url.PathAndQuery;
// 完整URL地址 //
string strAbsoluteUri = hc.Request.Url.AbsoluteUri;
int idx = strAbsoluteUri.LastIndexOf(strPathAndQuery);
if (idx == -1)
throw ThrowSlowXFunctionsExceptions.NewSlowXFunctionsExceptionIsNullMessage
(
MethodBase.GetCurrentMethod(),
"int idx = strAbsoluteUri[" + strAbsoluteUri + "].LastIndexOf(strPathAndQuery[" + strPathAndQuery + "]) == -1;"
);
return strAbsoluteUri.Substring(0, idx);
}
/// <summary>
/// 页面名称
/// list.aspx
/// </summary>
/// <returns></returns>
public static string GetCurPageName()
{
HttpContext hc = HttpContext.Current;
// 虚拟目录加完整参数页面地址 //
// /SlowXWebSite/Test/WebCommon/Default.aspx?id=default.aspx&web=%cb%aa%d2%b6&dt=D%3a%5ccanoe%5cs.aspx&p=fdf%5cf%2ffds.fdsf%3ffdf //
string strPathAndQuery = hc.Request.Url.PathAndQuery;
if (strPathAndQuery == null || strPathAndQuery.Length == 0)
return "";
int idx = strPathAndQuery.LastIndexOf("?");
if (idx != -1)
strPathAndQuery = strPathAndQuery.Substring(0, idx);
idx = strPathAndQuery.LastIndexOf("/");
if (idx != -1)
strPathAndQuery = strPathAndQuery.Substring(idx + 1);
return strPathAndQuery;
}
/// <summary>
/// 页面名称和参数
/// list.aspx?id=1
/// </summary>
/// <returns></returns>
public static string GetCurPageNameAndQuery()
{
HttpContext hc = HttpContext.Current;
// 虚拟目录加完整参数页面地址 //
// /SlowXWebSite/Test/WebCommon/Default.aspx?id=default.aspx&web=%cb%aa%d2%b6&dt=D%3a%5ccanoe%5cs.aspx&p=fdf%5cf%2ffds.fdsf%3ffdf //
string strPathAndQuery = hc.Request.Url.PathAndQuery;
string strQuery = "";
if (strPathAndQuery == null || strPathAndQuery.Length == 0)
return "";
int idx = strPathAndQuery.LastIndexOf("?");
if (idx != -1)
{
strQuery = strPathAndQuery.Substring(idx);
strPathAndQuery = strPathAndQuery.Substring(0, idx);
}
idx = strPathAndQuery.LastIndexOf("/");
if (idx != -1)
strPathAndQuery = strPathAndQuery.Substring(idx + 1);
return strPathAndQuery + strQuery;
}
/// <summary>
/// 页面名称_过滤后缀
/// list
/// </summary>
/// <returns></returns>
public static string GetCurPageOnlyName()
{
HttpContext hc = HttpContext.Current;
// 虚拟目录加完整参数页面地址 //
// /SlowXWebSite/Test/WebCommon/Default.aspx?id=default.aspx&web=%cb%aa%d2%b6&dt=D%3a%5ccanoe%5cs.aspx&p=fdf%5cf%2ffds.fdsf%3ffdf //
string strPathAndQuery = hc.Request.Url.PathAndQuery;
if (strPathAndQuery == null || strPathAndQuery.Length == 0)
return "";
int idx = strPathAndQuery.LastIndexOf("?");
if (idx != -1)
strPathAndQuery = strPathAndQuery.Substring(0, idx);
idx = strPathAndQuery.LastIndexOf("/");
if (idx != -1)
strPathAndQuery = strPathAndQuery.Substring(idx + 1);
idx = strPathAndQuery.LastIndexOf('.');
if (idx != -1)
strPathAndQuery = strPathAndQuery.Substring(0, idx);
return strPathAndQuery;
}
/// <summary>
/// PhyPath
/// /WebSite
/// </summary>
/// <returns></returns>
public static string GetPhyPath()
{
HttpContext hc = HttpContext.Current;
if (hc.Request.ApplicationPath == "/")
return "";
return hc.Request.ApplicationPath;
}
/// <summary>
/// 参数内容_带问号
/// ?id=1
/// </summary>
/// <returns></returns>
public static string GetUrlQueryParams()
{
HttpContext hc = HttpContext.Current;
string str = hc.Request.Url.Query;
if (str == null || str.Length == 0)
return "";
return str;
}
/// <summary>
/// 参数内容_不带问号
/// id=1
/// </summary>
/// <returns></returns>
public static string GetUrlQuery()
{
HttpContext hc = HttpContext.Current;
string str = hc.Request.Url.Query;
if (str == null || str.Length == 0)
return "";
if (str.StartsWith("?"))
return str.Substring(1);
return str;
}
/// <summary>
/// 是否本地
/// </summary>
/// <returns></returns>
public static bool IsLocalHost()
{
if (HttpContext.Current == null)
return false;
if (HttpContext.Current.Request == null)
return false;
return HttpContext.Current.Request.IsLocal;
}
/// <summary>
/// 当前页面所在根目录地址
/// /WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx
/// </summary>
/// <returns></returns>
public static string GetPagePath()
{
HttpContext hc = HttpContext.Current;
string strOne = hc.Request.Url.LocalPath;
string strTwo = "";
if (hc.Request.ApplicationPath != "/")
strTwo = hc.Request.ApplicationPath;
if (strOne == null || strOne.Length == 0)
return "";
if (strTwo == null || strTwo.Length == 0)
return strOne;
return strOne.Substring((strTwo).Length);
}
/// <summary>
/// 当前页面所在根目录地址_带参数
/// /WebForms/WebPages/code/dotnet/HttpContextInfo/Info/Default.aspx?id=1
/// </summary>
/// <returns></returns>
public static string GetPagePathAndParams()
{
HttpContext hc = HttpContext.Current;
string strOne = hc.Request.Url.PathAndQuery;
string strTwo = "";
if (hc.Request.ApplicationPath != "/")
strTwo = hc.Request.ApplicationPath;
if (strOne == null || strOne.Length == 0)
return "";
if (strTwo == null || strTwo.Length == 0)
return strOne;
return strOne.Substring((strTwo).Length);
}
/// <summary>
/// Ip地址
/// 127.0.0.1
/// </summary>
/// <returns></returns>
public static string GetIp()
{
HttpContext hc = HttpContext.Current;
return hc.Request.ServerVariables["REMOTE_ADDR"];
}
示例链接
标签:
获得Url的地址属性 


页面名称 


根目录地址 


ip地址 


URL地址 


访问地址 


上一条:
下一条:
相关评论
发表评论