.NET 絕對路徑的配置

來源:互聯網
上載者:User

標籤:blog   http   io   使用   ar   div   問題   sp   log   

      有時候因為用IIS配置網站,會導致一些全域引用有路徑問題無法引用到。今天就說一下,關於全域引用的絕對路徑的配置,譬如,由於IIS配置的虛擬路徑,一些CSS,JS的引用找不到,又或者自訂的一些跳轉出現404。這些問題很簡單也很基礎。

     我們可以定義一個BasePage類,把一些頁面公用的引用,提示資訊放在裡面,這樣每個頁面只要繼承這個類,就可直接使用BasePage裡的方法了(物件導向的三大特性之一:繼承都知道的吧!)。首先定義一個絕對路徑的字串

    /// <summary>    /// 定義絕對路徑    /// </summary>    public string ApplicationPath = "";

  然後初始化。注意一點,這裡使用的衍生類別實現基類的方法。使用override重寫此功能,使用base關鍵字繼承基類的方法。

    /// <summary>    ///初始化函數    /// </summary>    /// <param name="e"></param>    protected override void OnInit(EventArgs e)    {        base.OnInit(e);        InitApplication();    }

  最後實現絕對路徑的賦值

/// <summary>
/// 初始化請求路徑
/// </summary>
public void InitApplication()
{
  #region 定義ApplicationPage
  this.ApplicationPath = HttpContext.Current.Request.ApplicationPath == "/" ? "" : HttpContext.Current.Request.ApplicationPath + "";
  #endregion
}

  此時ApplicationPath 就是網站的根名稱。

.NET 絕對路徑的配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.