2017-01-25 57 views
0

所以我已經登錄LinkedIn並且正在嘗試添加Facebook。在foreach中顯示圖片

我已經有「簽到隨着LinkedIn」按鈕的工作,並正嘗試加入Facebook的一個:

<div id="socialLoginList"> 
       <p> 
        @foreach (AuthenticationDescription p in loginProviders) 
        { 
         var source = "~/Img/" + p.AuthenticationType + ".png"; 
         <input type="image" src="@source" border="0" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" style="max-height: 100%; max-width: 100%"> 
        } 
       </p> 
      </div> 

如果我不建立這樣的路徑,並給它SRC =「〜 /Img/Facebook.png「或src =」〜/ Img/LinkedIn.png「他們的工作。我該如何編寫路徑,以便圖片不會在頁面上顯示爲破碎的元素?

+0

那麼什麼是URL看起來像在提供的頁面時,它在工作時是壞的?我的猜測是,當你手動構建URL時,你需要自己解析'〜'。 –

回答

2
src="@Url.Content(String.Format("~/Img/{0}", source))" 
+0

大概你可以在這裏使用字符串插值? 'src =「@ Url.Content($」〜/ Img/{source})「'? –

+0

這工作,不需要第二個,謝謝! – crystyxn

1

如果您正在使用C#6.0比你可以用Interpolated Strings它會給你同樣的事情,但在更清潔的方式

src="@Url.Content($"~/Img/{@source}")"