2010-02-01 117 views
1

我正在構建一個使用主題的asp.net應用程序,並且我已經使用web配置爲應用程序分配了一個主題。在頭部內容和鏈接標記中使用代碼塊

我有一個書籤圖標,我想用於我的頁面,它位於主題目錄中,但我在引用標題中鏈接標記的主題位置時遇到問題。

首先,我試着把一個代碼塊放在鏈接標籤href元素裏面,這沒有奏效。相反,它所作的只是HTML的<%字符和輸出編碼直接到瀏覽器:

<link rel="shortcut icon" href="/App_Themes/<%=Page.Theme %>/images/bookmark.ico" type="image/x-icon" runat="server"/> 

我能夠把一個代碼塊的元素在裏面一個小時標籤了,所以我不知道爲什麼它不會工作在一個鏈接標籤:

<hr test="<%=Page.Theme %>"/> 

然後我試圖做head標籤內的Response.Write,但我得到了一個錯誤說Controls集合不能修改,因爲該控件包含代碼塊:

<% Response.Write("<link rel=\"shortcut icon\" href=\"/App_Themes/" + Page.Theme + "/images/bookmark.ico\" type=\"image/x-icon\"/>"); %> 

我也試過它只是用於字符串,並得到了同樣的錯誤:

<%= "<link rel=\"shortcut icon\" href=\"/App_Themes/" + Page.StyleSheetTheme + "/images/bookmark.ico\" type=\"image/x-icon\"/>" %> 

有什麼辦法引用從鏈接標籤內的主題目錄的東西嗎?

我想在ASP.NET 2和ASP.NET 2 MVC應用程序中都這樣做。

+0

你能告訴我們什麼是從你的第一個例子呈現給瀏覽器?如果您的應用程序存儲在一個子目錄中,則會因爲您的鏈接指向根目錄而彈出。如果這不是問題,呈現給瀏覽器的html可能會保存密鑰。 – 2010-02-01 16:03:58

+0

NickLarsen - 它會渲染<%= Page.Theme%>我的代碼塊被指定。 – Jeremy 2010-02-01 18:07:34

回答

1

這是行不通的,因爲你將其標記爲RUNAT = 「服務器」

試試這個,而不是

<link rel="shortcut icon" href="<%=ResolveUrl(string.Format("~/App_Themes/{0}/images/bookmark.ico", Page.Theme)) %>" type="image/x-icon"/> 
+0

也不起作用。仍然得到錯誤控件集合不能被修改,因爲控件包含代碼塊(即)。 – Jeremy 2010-02-02 18:50:57

+0

你能告訴我你整個標籤是什麼樣子嗎? – hunter 2010-02-02 19:02:52

+0

如果你的head標籤中有runat =「server」並且你添加了其他任何控件,那麼你不能按照我的建議去做 – hunter 2010-02-02 19:03:41

0

好的我通過使用一些內聯代碼得到它的工作。內聯代碼不是我的第一選擇,但我想要一個可以在mvc中工作的解決方案。以下是我想出了:

<head runat="server"> 
    <title>My Test App <title> 
    <script language="CS" runat="server"> 
     void Page_Load(object sender, System.EventArgs e) 
     { 
      string sTheme = String.IsNullOrEmpty(Page.Theme) ? Page.StyleSheetTheme : Page.Theme; 
      litFacIcon.Text = "<link rel=\"shortcut icon\" href=\"/App_Themes/" + sTheme + "/images/bookmark.ico\" type=\"image/x-icon\"/>"; 
     } 
    </script> 
    <asp:Literal ID="litFacIcon" runat="server"></asp:Literal> 
</head> 
1

傑里米,

嘗試創建一個html助手,例如:

public static string SetThemeIcon(this HtmlHelper html, string themename) 
{ 
    var filePath = VirtualPathUtility.ToAbsolute("~/App_Themes/" + themename + "/images/bookmark.ico"); 
    return "<link rel=\"shortcut icon\" href=\"" + filePath + "\" type=\"image/x-icon\"/>"; 
} 

那麼,在您看來或母版頁,簡單地引用它像這樣:

<%= Html.SetThemeIcon("test") %> 

或在您的案件上方(MVC):

​​3210

希望這有助於...

吉姆

1

您可以通過使用一些服務器元素包裹腳本擺脫內嵌代碼:

<head runat="server"> 
    <title>My Test App <title> 
    <div runat="server"> 
     <link rel="shortcut icon" href="/App_Themes/<%=Page.Theme %>/images/bookmark.ico" type="image/x-icon" runat="server"/> 
    </div> 
</head> 
+0

看起來您可以將runat = server放在元素上,但不能將綁定標記嵌入到href屬性的中間,因爲它們會以編碼html的形式呈現出來。如果我在開始時放置一個〜,它確實會呈現應用程序根目錄。 – Jeremy 2012-04-23 22:43:25

+0

感謝這個有用的提示。最好的祝福。 – 2017-10-12 10:02:22

0

你也可以這樣做:

<head> 
    <style type="text/css"> 
     @import "<%= ResolveUrl("~/content/styles.css") %>"; 
     @import "<%= ResolveUrl("~/content/print.css") %>" print; 
    </style> 
</head> 
0

我用這樣的方式。 (添加虛擬空間分成塊):

<link href="<%=""+RootUrl%>_ui/css/font-awesome.min.css" rel="stylesheet"> <link rel="shortcut icon" href="<%=""+RootUrl%>_ui/favicon.ico" />

相關問題