首页 > 代码库 > C# 获取当前IIS请求地址

C# 获取当前IIS请求地址

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// 页面请求Hepler
/// </summary>
public class HttpHelper
{
/// <summary>
/// 获得全路径
/// </summary>
/// <param name="strVirtualPath"></param>
/// <returns></returns>
public static string GetFullUrlPath(object virtualPath)
{
if (virtualPath == null)
{
return string.Empty;
}
string strVirtualPath = virtualPath.ToString();
if (string.IsNullOrEmpty(strVirtualPath))
{
return string.Empty;
}
if (strVirtualPath.Contains("javascript:"))
{
return strVirtualPath;
}
//如果是外部链接
if (strVirtualPath.Length > 7 && strVirtualPath.Substring(0, 7).ToUpper().Equals("HTTP://"))
{
return strVirtualPath;
}
if (strVirtualPath.Length > 2 && strVirtualPath[0] == ‘~‘ && strVirtualPath[1] == ‘/‘)
{
strVirtualPath = strVirtualPath.Remove(0, 2);
}
if (strVirtualPath.Length > 1 && strVirtualPath[0] == ‘/‘)
{
strVirtualPath = strVirtualPath.Remove(0, 1);
}
string webPath = HttpContext.Current.Request.ApplicationPath;
if (webPath != "/")
{
return string.Format("{0}/{1}", webPath, strVirtualPath);
}
else
{
return string.Format("{0}{1}", webPath, strVirtualPath);
}

}

/// <summary>
/// 获取网站URL
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string GetRequestWebUrl()
{
HttpRequest request = HttpContext.Current.Request;
string urlAuthority = request.Url.GetLeftPart(UriPartial.Authority);
if (request.ApplicationPath == null || request.ApplicationPath == "/")
{
//当前部署在Web站点下
return urlAuthority;
}
else
{
//当前部署在虚拟目录下
return urlAuthority + request.ApplicationPath;
}
}
}

C# 获取当前IIS请求地址