ASP.NET string interpolation for attributes - asp.net

I am simply trying to insert a variable into a favicon link
<link rel="shortcut icon" href="<%=Favicon%>" type="image/x-icon" />
It works when I don't have to quote the text.
I have Favicon defined in the Page_Load and I'm using interpolation to generate the page title without problem.

Try this instead...
Page:
<link runat="server" rel="shortcut icon" type="image/x-icon" id="favIcon" />
Page_Load:
favIcon.Attributes.Add("href", Page.ResolveUrl("~/PathTo/somecool.ico"));

So here's what I found. It seems to be an issue with the link tag in the head. Copy the exact same code and put it in the body and it works. Take the following and put in the head and it populates as expected: <div id="<%= Favicon %>"><%= Favicon %></div>
Work arounds:
Take quotes out of the element and put them in the string:
Markup: note no quotes for href
<link rel="shortcut icon" href=<%= Favicon %> type="image/x-icon" />
C#: quotes added to string
public string Favicon = "\"//www.alink.com/toAnImage.ico\"";
I think Scotty's approach may be better though

Try adding runat=server
<link runat="server" rel="shortcut icon" href="<%=Favicon%>" type="image/x-icon" />

Coming from ruby this seems a bit like overkill just to avoid allowing dynamic attributes within a website.
I did not really need to do this, just wanted to.
This is what I did, which could be totally the wrong way. I have no idea.
I deleted the style and favicon links entirely in the aspx.
In Page_Load:
public string GlobalStyleSheet, Favicon;
protected void Page_Load(object sender, EventArgs e)
{
GlobalStyleSheet = WebConfigurationManager.AppSettings["GlobalStyleSheet"];
Favicon = WebConfigurationManager.AppSettings["Favicon"];
HtmlHead head = (HtmlHead)Page.Header;
HtmlLink faviconLink = new HtmlLink();
HtmlLink cssLink = new HtmlLink();
faviconLink.Href = Favicon;
faviconLink.Attributes.Add("rel", "shortcut icon");
faviconLink.Attributes.Add("type", "image/x-icon");
Page.Header.Controls.Add(faviconLink);
cssLink.Href = GlobalStyleSheet;
cssLink.Attributes.Add("rel", "stylesheet");
Page.Header.Controls.Add(cssLink);
}

Related

Adding build version to css files in asp.net

I need to add the version at each css file, for that I have created a function which returns the build version, but when I add the function to the path it doesn't get properly rendered:
<!--Code-->
<link href="Styles/Site.css<% Version() %>" rel="stylesheet" type="text/css" />
<!--Render-->
<link href="Styles/Site.css<% Version() %>" rel="stylesheet" type="text/css" />
I tried both <% %> and <%= %> and even using a global variable instead of a public function but with no results, however I was able to add the version to js files through modifyng the path in the ScriptManager object.
You can always add a stylesheet programatically.
HtmlLink hl = new HtmlLink();
hl.Href = "Styles/Site.css" + Version();
hl.Attributes.Add("type", "text/css");
hl.Attributes.Add("rel", "stylesheet");
Page.Header.Controls.Add(hl);
The reason <%= %> does not work is because it is in the Head of the page, which is a Control in itself. If you put your sheet outside the Head, it does work.

Spring mvc: css does not work when adding slash at the end of URL

I am new to Spring MVC and i am having a problem with CSS. When the URL ends with slash CSS does not work.
link goes like this
<link rel="stylesheet" href="themes/style.css">
mvc:resources mapping
<mvc:resources mapping="/themes/**" location="/WEB-INF/themes/"/>
and requestMapping goes like this
#RequestMapping("/login")
public ModelAndView loginPage() {
ModelAndView model = new ModelAndView("login");
return model;
}
So the problem is when i enter a URL like ../login the css loads normally, but when i enter ../login/ with ending slash, then the css does not load.
Well there are many similar questions in here, but none of them is for Spring MVC.
Instead of
<link rel="stylesheet" href="themes/style.css">
try this:
<link rel="stylesheet" href="/themes/style.css">
When you use href="themes/style.css" then for url like: .../login/ the request url for css file looks like:
.../login/themes/style.css
which is incorrect. When you use href="/themes/style.css" then it always should result with:
.../themes/style.css
Update:
If it is jsp page, then add
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> on top of the page
and change:
<link rel="stylesheet" href="themes/style.css">
into
<link rel="stylesheet" href="<c:url value="/themes/style.css" />">
it will work 100%
step 1
<mvc:resources location="/WEB-INF/assets/" mapping="/resources/**"></mvc:resources>
setp 2
<spring:url var="css" value="/resources/css/"/>
<spring:url var="js" value="/resources/js/"/>
<spring:url var="image" value="/resources/image/"/>
add a slash after value
step 3
add your style sheet like this
<link rel="stylesheet"
href="${css}/common/bootstrap.min.css">

Css link can't call the variable from behind code in asp.net?

I write it in aspx like this:
<link type="text/css" href='/theme<%=theme%>/top.css' rel="stylesheet" />
but result is this:
<link type="text/css" href="/theme<%=theme%>/top.css" rel="stylesheet" />
surprising is that using in js is no problem.
It's because your link is inside a <head> tag which is runat server. The head tag is "smart", in that when it sees a <link> tag it lets you actually use the application relative path syntax: ~/. So if you do (inside your head tag):
<link href="~/Content/Site.css" rel="stylesheet" />
You'll see it will actually work (that is, it will expand the tilde to the proper location of your site), even though nowhere did you say runat server. The downside of course is when you DON'T want it to do that. :) Probably the easiest solution is to just manually build the tag yourself like this:
<%= "<link type=\"text/css\" href='/theme" + theme + "/top.css' rel=\"stylesheet\" />" %>
You can't use expressions with <head runat="server"> instead you have to write following code in Page_Load event to insert <link/>
HtmlLink link = new HtmlLink();
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "Stylesheet");
link.Attributes.Add("href", "/theme" + theme + "/top.css");
Header.Controls.Add(link);

What does "#{ some expression; } mean?

I was advised (by a person I cannot now contact to ask this) to use a query-string-trick to keep from caching a style sheet while I was debugging. The respondent said this would do the trick:
#{ var currentDate = DateTime.Now; }
<link href="#Url.Content("~/Styles/Site.css?" + currentDate)" rel="stylesheet" type="text/css" />
And I see why, but the expression #{ var currentDate = DateTime.Now; } is just resolving to the literal value in the page when I run it. The full code is:
<head runat="server">
<title></title>
#{ var currentDate = DateTime.Now; }
<link href="#Url.Content("~/Styles/Site.css?" + currentDate)" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
The syntax "#{ }" is new to me. I don't see a reference to it in any doc that I have looked at. According to the usage it appears to be inline script, but it isn't being treated as that at runtime, and I am not even sure if it is Active Server Page syntax (or PHP?).
What DOES work is:
<% var currentDate = DateTime.Now; %>
<link href="~/Styles/Site.css?<%= currentDate%>" rel="stylesheet" type="text/css" />
OK, but still, what does "#{ <some expression> }" signify?
It's Razor, which is a newer rendering engine for asp.net. It's doing the exact same thing as your <% %> block of code.
It is not an expression, it is a code block (a collection of one or more declarations and statements) in Razor syntax.
The reason it does not work for you is that you are not using the razor engine. # replaces the need for <% %> in asp.net by implementing the Razor engine, most notably through mvc3.

Root route breaking paths

I'm in the process of deploying an ASP.NET MVC app to IIS 6, but am running into an issue with the root path.
In Global.asax, I have the root path mapped:
routes.MapRoute("Root", "",
new { controller = "Dashboard", action = "Index", id = "" });
When I navigate to http://servername:70/test2/, the app displays the right page, but the stylesheet and JavaScript files are not loading. Looking at the source, the paths are showing like this:
<script src="test2/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css"
href="test2/Content/stylesheets/app.css" />
Which makes the browser look for
http://servername:70/test2/test2/Content/stylesheets/app.css
When I go directly to a controller (http://servername:70/test2/Dashboard.aspx), the paths are correct:
<link rel="stylesheet" type="text/css" href="Content/stylesheets/app.css" />
This is also occurring with any links generated with ActionLink. The stylesheet and script paths are being generated with Url.Content:
<link rel="stylesheet" type="text/css"
href="<%= Url.Content("~/Content/stylesheets/app.css") %>" />
I have recently answered a question similar to this which uses Rob Conery's Script Registration Helper. I'll copy the answer in here for you, and add an example HtmlHelper for stylesheets.
public static string RegisterJS(this System.Web.Mvc.HtmlHelper helper, string scriptLib) {
//get the directory where the scripts are
string scriptRoot = VirtualPathUtility.ToAbsolute("~/Scripts");
string scriptFormat="<script src=\"{0}/{1}\" type=\"text/javascript\"></script>\r\n";
return string.Format(scriptFormat,scriptRoot,scriptLib);
}
public static string RegisterCSS(this System.Web.Mvc.HtmlHelper helper, string styleLink, string rel) {
//get the directory where the css is
string stylesheetRoot = VirtualPathUtility.ToAbsolute("~/Content/Stylesheets");
string styleFormat="<link type='text/css' href='{0}/{1}' rel='{1}' />\r\n";
return string.Format(styleFormat, stylesheetRoot, styleLink, rel);
}
Usage:
<%= Html.RegisterJS("myscriptFile.js") %>
<%= Html.RegisterCSS("app.css") %>
Hope this helps.
Also, I note that there was another response on that question by Levi:
This should have been fixed in RC2. If you are using RC2 and are still seeing this problem, please file a bug at http://forums.asp.net/1146.aspx.
If this is your preferred answer, the please upvote Levi's response.
Or, use the Url.Content ...
<link href="<%=Url.Content("~/Content/Site.css") %>" rel="stylesheet" type="text/css" />

Resources