如何在 page.aspx 中引用 constants.cs 中的常量,我正在尝试以下操作但没有成功
<%@ Import Namespace="MyConstants" %>
<uc:MyControl ID="id1" runat="server" ConstantValue=" <%= Constants.TheValue %>" />
<uc:MyControl ID="id2" runat="server" ConstantValue=" <%# Constants.TheValue %>" />
<uc:MyControl ID="id3" runat="server" ConstantValue=" <%= MyConstants.Constants.TheValue %>" />
<uc:MyControl ID="id4" runat="server" ConstantValue=" <%# MyConstants.Constants.TheValue %>" />
并且在Constants.cs
namespace MyConstants
public class Constants
public const string TheValue = "Hello, World";
最佳答案
您需要导入您的命名空间。根据您的 View 引擎,您以不同的方式执行此操作。
如果您使用的是 WebForms:
<%@ Import Namespace="Your.Namespace" %>
如果您将 Razor 与 C# 结合使用
@using Your.Namespace
如果您将 Razor 与 VB.NET 一起使用
@Imports Your.Namespace
关于c# - 如何从 aspx 引用 cs 中的常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12795128/