英文:
Unable to use variable in the codebehind in the aspx
问题
我试图根据代码中收集的信息为我的页面加载自定义Favicon。我有一个名为ClientFavicon
的公共字符串属性,该属性在我的aspx文件的代码后台中正确填充,但似乎我无法使用它。以下是我的aspx文件的外观:
<%@ 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" />
...
其他头部元素
...
</head>
<body>
...
其他页面元素
...
</body>
</html>
以及代码后台:
namespace project.namespace
{
public class myclass
{
public string ClientFavicon { get; set; }
private void Page_Load(object sender, EventArgs e)
{
...
其他代码
...
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;
}
}
}
}
然而,当我运行这个代码时,我没有看到Favicon,并且在浏览器开发工具的网络选项卡中出现以下400错误:
https://website.url/aspxpage/%3C%=ClientFavicon%%3E
因此,在我看来,它似乎不将链接中的内容识别为变量,而只是文本。
对于Favicon链接元素,我已尝试但未成功的选项包括:使用单引号而不是双引号,删除等号,以及添加runat="server"
。
我还尝试在aspx页面的顶部放置了以下代码片段<% var test = ClientFavicon; %>
并设置断点,test
变量包含了正确的数据。我也无法使 test
在上述所有语法试验和错误中与href一起工作。
最后,我尝试将填充ClientFavicon
的方法移到代码后台之外,并放到<% %>
块中,放在<head>
标记之前的页面顶部,并在那里使用一个变量,但结果相同。
我查看了一些其他与此相关的StackOverflow帖子,但不确定还可以尝试什么或是否遗漏了什么。
英文:
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.
答案1
得分: 0
一位同事发现将`link`标签包裹在asp占位符中可以起作用,因为`<head>`具有`runat="server"`属性:
<asp:PlaceHolder runat="server">
<link href="<%=ClientFavicon%>" rel="shortcut icon" type="image/x-icon" />
</asp:PlaceHolder>
英文:
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>
答案2
得分: 0
尝试将链接更改如下:
<link href=<%=ClientFavicon%> rel="shortcut icon" type="image/x-icon" />
然后将引号包括在变量中:
ClientFavicon = "\"\"" + clientFolder + favicon + "\"\"";
您可能还需要设置整个 link
实体:
<%=ClientFavicon%>
然后在代码后端:
var q = "\"\"";
ClientFavicon = $"<link href={q}{clientFolder}{favicon}{q} rel={q}shortcut icon{q} type={q}image/x-icon{q} />";
由于这是Web Forms,您可能会使用一些过于老旧的字符串插值:
ClientFavicon = string.Format("<link href={0}{1}{2}{0} rel={0}shortcut icon{0} type={0}image/x-icon{0} />", "\"\"", clientFolder, favicon);
(我发现格式占位符比转义引号或原始字符串更容易阅读,但这是个人偏好。)
英文:
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.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论