在我的项目 ASP.NET C# 中,我有一些具有特定名称和值的常量字符串,例如:Constant string str1 = "VAL1";
Constant string str2 = "VAL2";我想在所有页面中使用这些字符串。最好的方法是什么?
2 回答

慕码人2483693
TA贡献1860条经验 获得超9个赞
我认为创建一个包含常量的类的专用类库,然后将该项目作为引用添加(或者甚至从该项目创建一个NuGet包并使用它)到所有需要这些常量的项目是最好的方法去。
该类可能如下所示:
public static class MyConstants
{
public const string str1 = "VAL1";
public const string str2 = "VAL2";
}
希望能帮助到你!

冉冉说
TA贡献1877条经验 获得超1个赞
这取决于这些字符串的用途,如果您将它们用于配置或类似原因,我建议将它们放在 web.config 中。您可以在 appSettings 中创建一个特定部分,例如:
<add key="HelpdeskContact" value="Please contact helpdesk on 0123456" />
添加回答
举报
0/150
提交
取消