c#
  • asp.net
  • webforms
  • .net-3.5
  • 2017-02-16 37 views 1 likes 
    1

    如何使用樣式-URL標記,HttpContext.Current.Request.Url.Host和HttpContext.Current.Request.Url.Authority on .aspx頁面(想要檢查它是否是localhost或不是直接)?三元條件之間的HttpContext.Current.Request.Url.Host和HttpContext.Current.Request.Url.Authority aspx頁

    這裏是例子:

    "div style='<%= "height: 1115px; background-image: url(\"" + "http://" + HttpContext.Current.Request.Url.Host.ToString() +"ImagePath"); background-position: bottom center; background-repeat: no-repeat;" %>'" 
    

    我要檢查它是否爲localhost,然後它會採取管理局,如果沒有,那麼它必須採取主機。

    +0

    你可以發表你想要的樣本。 –

    +0

    @PatrickHofman - 在我的項目中添加了一個div標籤。 –

    回答

    0

    此的.aspx代碼(抱歉,這是很長,我不能打破它分成多個不破壞語法):

    <div style='<%= "height: 1115px; background-image: url(\"http://" + (HttpContext.Current.Request.Url.Host.ToString().Contains("localhost") ? HttpContext.Current.Request.Url.Authority : HttpContext.Current.Request.Url.Host) + "/" + "path/to/image.jpg" + "\"); background-position: bottom center; background-repeat: no-repeat;" %>'>my_div_content</div> 
    

    會產生這樣的結果:

    <div style="height: 1115px; background-image: url("http://lc.host.com/path/to/image.jpg"); background-position: bottom center; background-repeat: no-repeat;">my_div_content</div> 
    

    給您ImagePath variabl包含圖像的真實路徑,你需要在div替換該行:

    + "path/to/image.jpg" + 
    

    這個:

    + ImagePath + 
    

    不要擔心,這兩個style="url("包含",應該反正工作。我在Chrome中確認了這一點。

    HTH

    +0

    它適合我。感謝您的合適解決方案! –

    -1

    僞代碼:

    HttpContext.Current.Request.Url.Contains( 「localhost」 的)? ... ...

    +0

    我喜歡沒有解釋的downvotes:D – Dexion

    相關問題