如何在ASP. js中使用变量?NET/Blazor _Layout.我在另一个类中创建的cshtml ?



我是SO NEWASP.NET/Blazor。我一直是Java的家伙许多多年来,我不得不转向这项新技术,所以请原谅我。

我有一个"常量"类别Constants.cs:

namespace MyAwesomeApplication.Web.Classes{
public class Constants{
public const String myString01 = "the quick brown fox";
public const String myString02 = "jumps over the lazy dog";
}
}

现在在我的_Layout.cshtml页面我想使用这些常数,但我不知道如何做到这一点。

<div>
@{// Use my constant here}
</div>

我尝试了Google搜索


编辑:我试过了:

<div>
@{MyAwesomeApplication.Web.Classes.Constants.myString01}
</div>

并收到错误:

CS0426: The type name 'myString01' does not exist in the type 'Constants'.

我想说的和gunr2171一样。

<div>
@(MyAwesomeApplication.Web.Classes.Constants.myString01)
</div>

,将写入文本。

也可以写成:

<div>
@MyAwesomeApplication.Web.Classes.Constants.myString01
</div>

但是如果你想做一些字符串连接,你需要使用括号()来响应写:

<div>
@(MyAwesomeApplication.Web.Classes.Constants.myString01 + " " + MyAwesomeApplication.Web.Classes.Constants.myString02)
</div>

或带代码块:

@{ 
string print = MyAwesomeApplication.Web.Classes.Constants.myString01 + " " + MyAwesomeApplication.Web.Classes.Constants.myString02
}
<div>
@print
</div>

最新更新