C# 擷取當前IIS請求地址

來源:互聯網
上載者:User

標籤:auth   equal   擷取   asc   外部連結   form   java   using   地址   

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請求地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.