简体   繁体   中英

How can I use a variable in the ASP.NET/Blazor _Layout.cshtml that I created in a different class?

I am SO NEW to ASP.NET/Blazor. I have been a Java guy for MANY years and have had to switch to this new technology, so please bear with me.

I have a "Constants" class 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";
    }
}

Now in my _Layout.cshtml page I would like to use those constants, but I do not know how to do it.

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

I tried a Google search , but didn't come up with much help.


EDIT: I have tried this:

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

And received the error:

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

Was going to say the same as gunr2171..

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

, will write the text.

Or you can write it as:

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

But if you want to do some string concationation, you need to use paranteses () for response write:

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

Or with a code-block:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM