0

I'm trying to get a custom favicon loaded for my page based on information gathered in the codebehind. I have a public string property ClientFavicon that is being populated correctly in the codebehind for my aspx file, but it seems I'm unable to use it. Here's what my aspx file looks like:

<%@ Page Language="c#" CodeBehind="page.aspx.cs" AutoEventWireup="True" Inherits="project.namespace" EnableViewState="True" Async="true" %>

<!DOCTYPE html>

<html lang="en">
<head runat="server">
    <meta charset="utf-8">
    <link href="<%=ClientFavicon%>" rel="shortcut icon" type="image/x-icon" />
    ...
    other head elements
    ...
</head>
<body>
    ...
    other page elements
    ...
</body>
</html>

And the codebehind:

namespace project.namespace
{
    public class myclass
    {
        public string ClientFavicon { get; set; }

        private void Page_Load(object sender, EventArgs e)
        {
           ... 
           other code
           ...

           LoadClientFavicon();
        }

        private void LoadClientFavicon()
        {
            var clientFolder = "/images/client/";
            var favicon = string.Empty;

            if (HttpContext.Current.Session != null)
            {
                favicon = HttpContext.Current.Session[Global.SESSION_GLOBAL_CLIENT_FAVICON] != null
                    ? HttpContext.Current.Session[Global.SESSION_GLOBAL_CLIENT_FAVICON].ToString()
                    : "favicon.ico";
                ClientFavicon = clientFolder + favicon;
            }
        }
    }
}

However when I run this, I get no favicon and the following 400 error in the browser devtools Network tab:

https://website.url/aspxpage/%3C%=ClientFavicon%%3E

So it looks to me like it doesn't recognize what's in the link as a variable, just as text.

For the favicon link element, I've tried with no success: using single quotes instead of double, removing the equals sign, and adding runat="server".

I've also tried placing a snippet <% var test = ClientFavicon; %> at the top of the aspx page and set a breakpoint, the test variable has the right data. I was also unable to get test to work in the href with all of the syntax trial and error above.

Lastly I've tried moving the method populating ClientFavicon out of the codebehind and into a <% %> block at the top of the page before the <head> tag and using a variable there, but I get the same result.

I've looked at a handful of other StackOverflow posts related to this and I'm not sure what else to try or if I'm missing something.

zakparks31191
  • 919
  • 2
  • 21
  • 42
  • you're not showing enough code.. where is variable defined? – T McKeown Jul 06 '23 at 16:46
  • what is `ClientFavIcon` is it string? According to the code it is a property of the page class.. show your code. – T McKeown Jul 06 '23 at 16:47
  • Your `CodeBehind` (CodeBehind="page.aspx.cs") and `Inherits` (Inherits="project.namespace") property values seem weird, are those the correct values? _"use the Inherits and CodeFile attributes to link the code-behind file to the .aspx file. In this example, the Inherits attribute indicates the MyCodeBehind class and the CodeFile attribute indicates the path to the language-specific file that contains the class."_ ... [Example](https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page?view=netframework-4.8.1#examples) – quaabaam Jul 06 '23 at 16:48
  • it is a public string property in the codebehind, i mention in the beginning. I can edit the code for it into the question – zakparks31191 Jul 06 '23 at 16:50
  • "page.aspx.cs" and "project.namespace" are obfuscated from their actual names – zakparks31191 Jul 06 '23 at 16:51
  • @TMcKeown i edited it in already. The variable ClientFavicon loads the correct value, I can see it when i set breakpoints – zakparks31191 Jul 06 '23 at 17:07
  • it should work.. so you must not be showing us something or you are getting error you are unaware of. – T McKeown Jul 06 '23 at 17:19
  • looks like this is a homework assignment? https://stackoverflow.com/questions/6644684/do-you-have-to-include-link-rel-icon-href-favicon-ico-type-image-x-icon – T McKeown Jul 06 '23 at 17:22
  • this is not a homework assignment, also not sure what the relevance is of that linked question. I have a default favicon in the root dir of the site already if that's what you mean – zakparks31191 Jul 06 '23 at 17:25
  • its been awhile for aspx for me... the async=true on the page make any difference? – T McKeown Jul 06 '23 at 17:27
  • removing it didn't change anything – zakparks31191 Jul 06 '23 at 17:29

2 Answers2

0

A coworker found that wrapping the link in an asp placeholder would work Since the <head> has runat="server":

<asp:PlaceHolder runat="server">
    <link href="<%=ClientFavicon%>" rel="shortcut icon" type="image/x-icon" />
</asp:PlaceHolder>
zakparks31191
  • 919
  • 2
  • 21
  • 42
0

Try changing the link like this:

<link href=<%=ClientFavicon%> rel="shortcut icon" type="image/x-icon" />

And then including the quotes as part of the variable:

ClientFavicon = "\"" + clientFolder + favicon + "\"";

You might also need to set the entire link entity:

<%=ClientFavicon%>

and then in the code-behind:

var q = "\"";
ClientFavicon = $"<link href={q}{clientFolder}{favicon}{q} rel={q}shortcut icon{q} type={q}image/x-icon{q} />";

And since this is Web Forms where you might be using something too old for string interpolation:

ClientFavicon = string.Format("<link href={0}{1}{2}{0} rel={0}shortcut icon{0} type={0}image/x-icon{0} />", "\"", clientFolder, favicon);

(I find the format placeholders are much more readable than escaped quotes or verbatim strings for including the quote characters, but that's a personal preference.)

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794