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)

3
  • 1
    have you tried adding parentheses, like '@(mynamespace.Controllers.MyVars.baseUrl)Tickets/Create'? Commented Feb 28, 2014 at 12:17
  • take a look at stackoverflow.com/questions/372865/path-combine-for-urls Commented Feb 28, 2014 at 12:21
  • @elolos. Right answer. thanks Commented Feb 28, 2014 at 12:21

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.