Can I use code blocks in head content and link tags to reference themes?
I'm building an ASP.NET app using themes, and I've assigned a theme to the app using the web config.
I have a bookmark icon that I want to use for my page and it is located in the themes directory, but I am having trouble referencing the themes location from a link tag in my header.
First I tried putting a code block inside the link tags href element, which did not work. Instead all it did was HTML encode the <% characters and output it directly to the browser:
<link rel="shortcut icon" href="/App_Themes/<%=Page.Theme %>/images/bookmark.ico" type="image/x-icon" runat="server"/>
I am able to put a code block inside an element in an hr tag though, so I don't know why it won't work in a link tag:
<hr test="<%=Page.Theme %>"/>
Then I tried doing a Response.Write inside the head tag, but I got an error saying the Controls col开发者_JAVA技巧lection cannot be modified because the control contains code blocks:
<% Response.Write("<link rel=\"shortcut icon\" href=\"/App_Themes/" + Page.Theme + "/images/bookmark.ico\" type=\"image/x-icon\"/>"); %>
I also tried it just with a string literal, and got the same error:
<%= "<link rel=\"shortcut icon\" href=\"/App_Themes/" + Page.StyleSheetTheme + "/images/bookmark.ico\" type=\"image/x-icon\"/>" %>
Is there any way to reference something from the themes directory inside the link tag?
I'm trying to do this in both an ASP.NET 2 and an ASP.NET 2 MVC app.
This wouldn't work because you marked it as runat="server"
try this instead
<link rel="shortcut icon" href="<%=ResolveUrl(string.Format("~/App_Themes/{0}/images/bookmark.ico", Page.Theme)) %>" type="image/x-icon"/>
You can get rid of the inline code by wrapping your script with some server element:
<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>
I use this way. (Add dummy space into block):
<link href="<%=""+RootUrl%>_ui/css/font-awesome.min.css" rel="stylesheet">
<link rel="shortcut icon" href="<%=""+RootUrl%>_ui/favicon.ico" />
Try creating an HTML helper such as:
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\"/>";
}
then, in your view or master page, simply reference it as so:
<%= Html.SetThemeIcon("test") %>
or as in your case above (in mvc):
<%= Html.SetThemeIcon(String.IsNullOrEmpty(Page.Theme) ? Page.StyleSheetTheme : Page.Theme) %>
Ok I got it working by using some inline code. Inline code is not my my first choice but I wanted a solution that worked in mvc as well. Here's what I came up with:
<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>
You could also do this:
<head>
<style type="text/css">
@import "<%= ResolveUrl("~/content/styles.css") %>";
@import "<%= ResolveUrl("~/content/print.css") %>" print;
</style>
</head>
精彩评论