<link href="<%=this.faviconURL %>" type="image/x-icon" rel="icon" />
It will literally print out '
<link href="%3C%25=this.faviconURL%20%25%3E" type="image/x-icon" rel="icon" />
It's as if the runtime is taking the link tag literally and refusing to do any sort of substitution. Can someone explain why this is happening? I would really prefer not using web controls for this.
I think you should add runat to link tag. So It can be processed by Asp.Net.
<link href="<%=this.faviconURL %>" runat="server" type="image/x-icon" rel="icon" />
Apperantly I was wrong see Scriptlet is not working in head tag and you can simple use without runat tag.
<link href="~/server/icofileurl" type="image/x-icon" rel="icon" />
How to add favicon.ico in ASP.NET site
Related
This is the code i call in _Layout.cshtml :
<head>
<link rel="icon" runat="server" href=#Url.Action("~/myicon.ico") type="image/ico" />
</head>
But icon don't show . I only get this default icon
favicon.ico
I think you want Url.Content, not Url.Action
<link rel="icon" runat="server" href=#Url.Content("~/myicon.ico") type="image/ico" />
Url.Content resolves the path and returns the URL. Url.Action is used when you actually want to call a controller action and do something with the result.
Maybe its a stupid question, but i'm having this issue in Visual Studio 2010:
in my Master page i've this code:
<head runat="server">
<title>App Title</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<link href="<%= App.RootPath %>Css/style.css" rel="stylesheet" type="text/css" />
</head>
for some strange reason the <% is changed at runtime with <%
<%= App.RootPath %> works normal if put anywhere outside head tag.
Anyone has never experienced this and resolved?
UPDATE:
If i put off runat="server" in head tag, it works. But i need it.
Edit:
All of these methods work, but the problem is lack of designer support?
The explanation for your trick:
<link <%= "href='" +App.RootPath +"Css/style.css'" %> rel="stylesheet" type="text/css" />
To find the answer generate a compilation exception. Change App.RootPath to App.RootPaths.., then navigate to the source code (it will be shown in the error page). If the compiler matches something like <link href='' rel='' > then it will generate the code to build a corresponding HtmlLink instance. So this is why it parses <%= as a literal string and after that it encodes it.
Your trick cheats the compiler, which is not bad at all.
I believe it does the same thing for meta tags, (HtmlMeta)
For now, i've found this workaroud; still searching for the reason of this behaviour.
<link <%= "href=" +App.RootPath +"Css/style.css" %> rel="stylesheet" type="text/css" />
This should work too.
<link href="<%= App.RootPath + "Css/style.css" %>" rel="stylesheet" type="text/css"/>
I normally use ResolveUrl:
<link href='<%= Page.ResolveUrl("~Css/style.css") %>' rel="stylesheet" type="text/css"/>
**problem**
<link rel="canonical" href="http://www.kayserianadoluhaber.com.tr/haber/<%=kanonikal%>" />
**solved**
<link rel="canonical" href="http://www.kayserianadoluhaber.com.tr/haber/<%=kanonikal+""%>" />
I want to add a version number to my js files.
<link href="css/reset.min.css?v=<%= App.Golbal.VERSION %>" media="all" rel="Stylesheet" type="text/css" />
This renders as
<link href="css/reset.min.css?v=<%= App.Golbal.VERSION %>" media="all" rel="Stylesheet" type="text/css" />
[Standard asp.net 4 web applciation]
Can anybody help?
Put it inside PlaceHolder control because link in the title not included in the form tag so no parsing will occur to it as following
<asp:PlaceHolder runat="server">
<link href="css/reset.min.css?v=<%= App.Golbal.VERSION %>" media="all" rel="Stylesheet" type="text/css" />
</asp:PlaceHolder>
As Dante suggests above, maybe change
<%= App.Golbal.VERSION %>
to
<%=App.Golbal.VERSION%>
or
<%=App.Global.VERSION%>
and try that.
Alternatively, like William suggest, set id and runat=server on the link element and apply the value in the server script/code behind.
<link id="lnkCSS" runat="server" media="all" rel="Stylesheet" type="text/css" />
and the server script/code behind, something like
//might need HtmlLink lnkCSS = FindControls("lnkCSS")`
lnkCSS.href = "css/reset.min.css?`v=" + App.Global.VERSION;
I've had similar issues before the way to get around it is make the link an asp:hyperlink and build the link in the code behind and then assign the link to the NavigateURL of the hyperlink.
Can you try removing the white space in "<%= App.Golbal"?
Btw, Global is mispelled :)
why in aspx ~ not working when i try to use it in JS
<script type="text/javascript" src="~/js/jQuery/jquery-ui.min.1.7.3.js"></script>
in source code is the same ~/js
but with CSS works
<link href="~/css/confirm.css" rel="stylesheet" type="text/css" media="screen" />
"~/" -substitution for the application root in ASP.NET, should work only with ASP.NET server controls
For example in ASP.NET MVC you may go like that:
<link href="#Url.Content("~/Content/Css/Site.css")" rel="stylesheet" type="text/css" />
<script src="#Url.Content("~/Content/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
You can view method Url.Content (System.Web.Mvc.dll, v4.0.30319, class UrlHelper) in Reflector.
Or you may try like that:
<link href="/Content/Css/Site.css" rel="stylesheet" type="text/css" />
<script src="/Content/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
~ only works for server controls, AFAIK. Don't know why it works for your CSS include.
This question already has answers here:
ASP.Net Master Page and File path issues
(10 answers)
Closed 9 years ago.
We're having some trouble with including javascript in masterpages. The "~/" root shortcut doesn't seem to work, and so we're having to manually enter in the path to the javascript from where it will be used, for example: "../../tooltip.js"
However, the problem is that if the nested page is in a different path, this approach does not work as the path stays the same despite the nested page being in a different location - any solutions on how to get the path automatically working like it does for the css files?
Thanks!
The ~/ is a special entity in ASP.NET which represents the "application root". The translation only happens when you pass that URL through an ASP.NET server control, such as <asp:Image runat="server" ImageUrl="~/path..." />. Trying to use it in something that is passed as literal text directly to the client, such as ` will be rendered exactly that in the browser.
There are a few solutions to this:
Put your scripts in a predictable place relative to the domain root, such as domain.com/scripts/script.js, and you can refer to it as /scripts/script.js. The preceding / tells the client (browser) it is an absolute path from the domain root.
Use Page.ResolveClientUrl to render the correct path (<%=Page.ResolveClientUrl("~/script./js")%>)
Create your own server control which handles the ~/ resolution.
Embed the script as an assembly resource.
Cory Larson recommends:
<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/tooltip.js") %>"></script>
Try
<script type="text/javascript" src=<%=Request.ApplicationPath+"/Scripts/Script_Name"%>></script>
I've been using url rewriting and "~" occasionally chokes on special characters in the url even when they are encoded.
If the script tag is in the HEAD element just use a path that is relative to the master page and asp.net will automatically fix the reference for you. Just make sure that the HEAD element has the runat=”server” attribute.
This also works really well for CSS files. It's the only way I've been able to get them to resolve in the VS.NET designer.
Here is a sample from my project:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../styles/reset.css" />
<link rel="stylesheet" type="text/css" href="../styles/960.css" />
<link rel="stylesheet" type="text/css" href="../styles/default.css" />
<link rel="stylesheet" type="text/css" href="../styles/buttons.css" />
<script type="text/javascript" src="../jQuery/jquery-1.3.2.js"></script>
</head>
Linked style sheets can be put within the header (with runat="server") and use a ~ to resolve to the root. However, Javascript files cannot be referenced this way. One additional way to add scripts is with a script manager in the body.
<html>
<head runat="server">
<title>title</title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery-1.4.2.js" />
<asp:ScriptReference Path="~/Scripts/jquery-ui-1.8.custom.min.js" />
</Scripts>
</asp:ScriptManager>
</body>
</html>
As answered # [Include Javascript adn CSS][1]
http://www.codeproject.com/Articles/404942/Include-JavaScript-and-CSS-on-your-MasterPage
For CSS files:
<link href="<%# ResolveUrl("~/") %>css/custom-theme/jquery-ui-1.8.21.custom.css" rel="stylesheet" type="text/css" />
For JavaScripts:
<script src="<%# ResolveUrl("~/") %>Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
Can you explain what you mean by the "~/" isn't working? It should be exactly what you're looking for. You might check that your head tag has the runat="server" attribute, since that might prevent the "~/" from working.