无法在aspx的codebehind中使用变量。

huangapple go评论58阅读模式
英文:

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:

&lt;%@ Page Language=&quot;c#&quot; CodeBehind=&quot;page.aspx.cs&quot; AutoEventWireup=&quot;True&quot; Inherits=&quot;project.namespace&quot; EnableViewState=&quot;True&quot; Async=&quot;true&quot; %&gt;

&lt;!DOCTYPE html&gt;

&lt;html lang=&quot;en&quot;&gt;
&lt;head runat=&quot;server&quot;&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;link href=&quot;&lt;%=ClientFavicon%&gt;&quot; rel=&quot;shortcut icon&quot; type=&quot;image/x-icon&quot; /&gt;
    ...
    other head elements
    ...
&lt;/head&gt;
&lt;body&gt;
    ...
    other page elements
    ...
&lt;/body&gt;
&lt;/html&gt;

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 = &quot;/images/client/&quot;;
            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()
                    : &quot;favicon.ico&quot;;
                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=&quot;server&quot;.

I've also tried placing a snippet &lt;% var test = ClientFavicon; %&gt; 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 &lt;% %&gt; block at the top of the page before the &lt;head&gt; 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占位符中可以起作用,因为`&lt;head&gt;`具有`runat=&quot;server&quot;`属性:

&lt;asp:PlaceHolder runat=&quot;server&quot;&gt;
    &lt;link href=&quot;&lt;%=ClientFavicon%&gt;&quot; rel=&quot;shortcut icon&quot; type=&quot;image/x-icon&quot; /&gt;
&lt;/asp:PlaceHolder&gt;
英文:

A coworker found that wrapping the link in an asp placeholder would work Since the &lt;head&gt; has runat=&quot;server&quot;:

&lt;asp:PlaceHolder runat=&quot;server&quot;&gt;
    &lt;link href=&quot;&lt;%=ClientFavicon%&gt;&quot; rel=&quot;shortcut icon&quot; type=&quot;image/x-icon&quot; /&gt;
&lt;/asp:PlaceHolder&gt;

答案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:

&lt;link href=&lt;%=ClientFavicon%&gt; rel=&quot;shortcut icon&quot; type=&quot;image/x-icon&quot; /&gt;

And then including the quotes as part of the variable:

ClientFavicon = &quot;\&quot;&quot; + clientFolder + favicon + &quot;\&quot;&quot;;

You might also need to set the entire link entity:

&lt;%=ClientFavicon%&gt;

and then in the code-behind:

var q = &quot;\&quot;&quot;;
ClientFavicon = $&quot;&lt;link href={q}{clientFolder}{favicon}{q} rel={q}shortcut icon{q} type={q}image/x-icon{q} /&gt;&quot;;

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

ClientFavicon = string.Format(&quot;&lt;link href={0}{1}{2}{0} rel={0}shortcut icon{0} type={0}image/x-icon{0} /&gt;&quot;, &quot;\&quot;&quot;, 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.)

huangapple
  • 本文由 发表于 2023年7月7日 00:21:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定