Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenation of c#razor string with html string with trailing slash [duplicate]

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)

like image 461
Sami Avatar asked Feb 28 '14 12:02

Sami


People also ask

Does C have concatenation?

As you know, the best way to concatenate two strings in C programming is by using the strcat() function.

Can I add strings in C?

In C, "strings" are just plain char arrays. Therefore, you can't directly concatenate them with other "strings".


1 Answers

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>
like image 165
CodingIntrigue Avatar answered Nov 01 '22 08:11

CodingIntrigue