Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用<;%=%时的不同输出&燃气轮机;_C#_Asp.net_Css - Fatal编程技术网

C# 使用<;%=%时的不同输出&燃气轮机;

C# 使用<;%=%时的不同输出&燃气轮机;,c#,asp.net,css,C#,Asp.net,Css,为了获得应用程序的完全限定路径,我编写了一个函数: public class Generic { public static string FullyQualifiedApplicationPath { get { //Return variable declaration string appPath = string.Empty;

为了获得应用程序的完全限定路径,我编写了一个函数:

    public class Generic
    {
        public static string FullyQualifiedApplicationPath
        {
            get
            {
                //Return variable declaration
                string appPath = string.Empty;

                //Getting the current context of HTTP request
                var context = HttpContext.Current;

                //Checking the current context content
                if (context != null)
                {
                    //Formatting the fully qualified website url/name
                    appPath = string.Format("{0}://{1}{2}{3}",
                                            context.Request.Url.Scheme,
                                            context.Request.Url.Host,
                                            context.Request.Url.Port == 80
                                                ? string.Empty
                                                : ":" + context.Request.Url.Port,
                                            context.Request.ApplicationPath);
                }

                if (appPath.EndsWith("/"))
                    appPath = appPath.Substring(0, appPath.Length - 1);

                return appPath;
            }
        }

    }
当我在
之间的
标记中使用它时,我得到了不同的输出

<link href="<%= Generic.FullyQualifiedApplicationPath %>/Styles/StyleSheet.css" rel="stylesheet" type="text/css" />
<script src="<%= Generic.FullyQualifiedApplicationPath %>/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>

html输出:

<link href="&lt;%= Generic.FullyQualifiedApplicationPath %>/Styles/StyleSheet.css" rel="stylesheet" type="text/css" />
<script src="http://localhost:2093/SourceOne/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>

只是想知道为什么asp.net引擎向客户端发送
%=Generic.FullyQualifiedApplicationPath%>


--NJ

ASP.NET基本上将内联求值视为字符串文字,并对其进行转义。您可以尝试使用数据绑定语法


<> p>重要的是绑定语法不与其他文字混合,否则ASP.NET会把整个属性当作字符串文字对待。

这是因为在头标签链接中考虑了服务器控件和服务器控件中的HRAF自动解码。

您可以在
a
标记上选中addrunat属性

<a runat="server" href='<%= Generic.FullyQualifiedApplicationPath %>/Styles/StyleSheet.css'></a>

在属性中与字符串文字混合时的任何时间。ASP.NET会将整个属性视为字符串文字并转义整个字符串。虽然在代码隐藏中分配它是一种解决方案,但我通常发现使用数据绑定语法是一种更干净的设计逻辑分离。是的,您的解决方案看起来更干净,感谢这个更好的解决方案。:)
lnkStyle.DataBind();
scptJQuery.DataBind();
<a runat="server" href='<%= Generic.FullyQualifiedApplicationPath %>/Styles/StyleSheet.css'></a>
<a href="&lt;%= Generic.FullyQualifiedApplicationPath %>/Styles/StyleSheet.css" ></a>
<link rel="stylesheet" type="text/css" runat="server" id="mystyle" />
mystyle.Href= Generic.FullyQualifiedApplicationPath + "/Styles/StyleSheet.css";