7

I have baseUrl = "http://localhost:10232";

I'm using it in my view like the following:

<a href='@mynamespace.Controllers.MyVars.baseUrl/Tickets/Create'>Create New</a>

It gives me fine output i.e

<a href='http://localhost:10232/Tickets/Create'>Create New</a>

But if I add / at end of my url like http://localhost:10232/

Then is there a way to produce same result like above? I tried it following way

<a href='@mynamespace.Controllers.MyVars.baseUrl+Tickets/Create'>Create New</a>

but concatenation does not work in html, so how can i achieve it (concat a c# variable with html string)

Sami
  • 8,168
  • 9
  • 66
  • 99

1 Answers1

25

Wrap it in parenthesis and the static part in quotes:

<a href='@(mynamespace.Controllers.MyVars.baseUrl+"Tickets/Create")'>Create New</a>
          ^                                       ^              ^^

This tells Razor anything inside the @() is one statement, allowing you to put C# in to concatenate the string.

Or if your last part is always static, you can leave out the quotes and move the text outside the parenthesis:

<a href='@(mynamespace.Controllers.MyVars.baseUrl)Tickets/Create'>Create New</a>
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176