-2

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>
N. Schor
  • 23
  • 2
  • `var myURL = $"Here"` – Matt Burland Nov 15 '16 at 19:53
  • Please use proper methods to construct Urls. In case of ASP.Net MVC you likely looking for `Html.Action` – Alexei Levenkov Nov 15 '16 at 19:58
  • Why is this the url I am getting localhost:53008/authentication/confirmhire?Cid=%E2%80%8C%E2%80%8B%7BconfirmationId%7D – N. Schor Nov 15 '16 at 20:10
  • If this is still happening it's because a url can't have certain charectors. For instance no spaces are allowed. When a url contains something not allowed it automatically gets escaped and turned into a predefined code (google can give all the escaped values). So since you are seeing those escaped codes, it simply means your url has invalid characters. You can simply leave it as is and it will work or.... if you want readable urls you can change the url and remove the invalid things, like spaces. – Jeremy Styers Nov 17 '16 at 16:24

1 Answers1

1

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.

Jeremy Styers
  • 497
  • 5
  • 23
  • Thank You! It's not easy being a newbie :) – N. Schor Nov 15 '16 at 20:21
  • 1
    Ok, next lesson. Religious war. The above comment is a perfect example and is when something that works correctly is debated simply because someone "thinks" they have a more correct solution. Since the question was merely "how do I insert this", my answer is fine and can be used in any code. Insert a into string b was the asked question and string formatting and interpolation is designed for that exact purpose. Since the question doesn't ask about querying params or anything above inserting, the answer fits. If character escaping or more advanced things are needed then a Uri is the way. – Jeremy Styers Nov 17 '16 at 16:40