razorblazor

Blazor Parameter reference with at symbol but within a text


How to make @Text act as Parameter but within a text?

For example, here

<div class="alert alert-@AlertClassType">some@Text</div>

some@Text is treated as a plain text,
but when string Text="thing" I'd expect 'something' in the result. I am clear, am I?

If I'd do

<div class="alert alert-@AlertClassType">some @Text</div>

it will "work" but that extra space before @ will also appear in the rendered content,
how to avoid that?


Solution

  • @ symbol denotes the start of C# code block, if used with existing markup then you will need to specify that the @ you're using is a code block explicitly by using ().

    string Text = "Thing";
    
    <div class="alert">some@(Text)</div>
    
    // renders: someThing
    

    Another tip, If you want to specify that the @() you're using is not a code block then you can use the @@ escape sequence.

    <div class="alert">some@@(Text)</div>
    
    // renders: some@(Text)
    

    Snippet