1

这是我想做的一个例子:

内容方面(Structure.master):

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Structure.master.cs" Inherits="Structure" %>

<!DOCTYPE HTML>
<html>
<head runat="server">
    <title>Bienvenue sur</title>
    <meta charset="utf-8" content="" />
    <link runat="server" href="App_Themes/Global/Metro.css?v=22" rel="stylesheet" type="text/css" />
    <link runat="server" href="App_Themes/Global/Site.css?v=<%=Version %>" rel="stylesheet" type="text/css" />
    <link runat="server" href="App_Themes/Global/Structure.css?v=22" rel="stylesheet" type="text/css" />

和代码隐藏(Structure.master.cs):

using System;
using System.Linq;
using BaseInstanceEntity = Library.Common.Entities.BaseEntities.BaseInstanceEntity;
using BaseInstanceManager = Library.Manager.BaseInstanceManager;

public partial class Structure : System.Web.UI.MasterPage
{
    public string Version { get; set;}

    protected void Page_Load(object sender, EventArgs e)
    {
        Version = System.Configuration.ConfigurationManager.AppSettings["Version"].Replace(".", "");

如图所示,我想将Versionvar 显示到标题中,作为缓存控件。当我执行上面的代码时,该行的结果是:

<link href="../App_Themes/Global/Site.css?v=&lt;%=Version %>" rel="stylesheet" type="text/css" />

就像代码没有被解释一样。我无法找到解释。为什么会这样?

4

3 回答 3

1

好吧,我迟到了这个问题,但是当我需要动态创建 CSS 链接时,我只需在后面的代码上生成整个标签,然后将其植入标记上,如下所示:

public partial class WebForm1 : System.Web.UI.Page
{
    public string link;
    protected void Page_Load(object sender, EventArgs e)
    {
        string ver = "1.0.0";
        link = "<link rel=\"stylesheet\"  href=\"../App_Themes/Global/Site.css?v=" + ver + "\"/>";
    }
}

然后标记

 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<%= link %>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

希望这将帮助其他来到此页面的人!

于 2013-05-14T23:29:38.693 回答
0

也许它应该是这样的:

<link runat="server" 
      href='<%# string.Format("{0}?v={1}", Page.ResolveUrl("~/App_Themes/Global/Site.css"), Version)%>' 
      type="text/css" />
于 2013-04-17T08:47:18.643 回答
0
    public static string Version
    {
        get
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            return String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
        }
    }
于 2015-02-08T13:18:11.063 回答