4thSpace
4thSpace

Reputation: 44310

How to concatenate two strings in image source with razor?

I've defined the following variable in razor:

@{string imageRoot = "/_media/Images/";}

I'd like to use it here:

<img src="@imageRoot App1/MyImage.png"/>

The problem is the space within the string.

This will work but I'd like to keep the trailing slash in the variable instead of in the literal:

@{string imageRoot = "/_media/Images";}
<img src="@imageRoot/App1/MyImage.png"/>

Upvotes: 1

Views: 3210

Answers (2)

Backs
Backs

Reputation: 24903

Looks a little ugly, but works:

@{string imageRoot = "/_media/Images/";}
<img src="@Html.Raw(imageRoot)App1/MyImage.png" />

Upvotes: 5

mahlatse
mahlatse

Reputation: 1307

Use String.Concat to merge the two string.

@{imgRoot = string.Concat(imgRoot,"/_media/Images");}

or just normal imgRoot + = imgRoot + "/_media/Images" then set your variable as <img src='@imageRoot'/> or <img src="@{imageRoot+@"/App1/MyImage.png"}"/> Note that the @-symbol is a Verbatim string literal and will prevent the \ from being escaped should you have a character that escapes your string

Upvotes: 0

Related Questions