N. Schor N. Schor - 7 months ago 48
C# Question

Putting variable into url

I am using mvc and trying to creat a url in the controller.
How do I put the variable confirmationId into the url?

"<a href=http://localhost:53008/authentication/confirmhire?Cid= + confirmationId'>Here</a>


Oh silly willy.

You're getting pounded with down votes because tis a simple question and answer.

If you're doing this in a controller, all you're doing is making a string in c#.

A string doesn't care about that plus sign you have. It thinks it's part of the string.

Also, single quote up there has no matching quote and is part of the string.

There's multiple ways to fix this. You can go basic, formatted, or interpolation.

You tried basic which would correctly be:

"<a href=\"http://localhost:53008/authentication/confirmhire?Cid=" + confirmationId + "\" > Here </ a > ";

Notice it's broken up.

A better way would be a formatted string as such:

string.Format("<a href=\"http://localhost:53008/authentication/confirmhire?Cid={0}\" > Here </ a > ", confirmationId);

And even better interpolation! :D

$"<a href='http://localhost:53008/authentication/confirmhire?Cid=‌​{confirmationId}'>He‌​re</a>"

Chose your poison, all are correct, though the last one is technically the best.