I'm trying to build an Href using Razor The string is going to end up looking like this:
In my model I have the order id and the hash string As the route is not a part of my site I don't believe I can use the default methods like @Url.Action and therefore can't use protocol: Request.Url.Scheme like I've used elsewhere.
So at present I'm trying to figure out how to create this using string functions I've tried Url.Encode Url.EscapeDataString Html.Encode but am getting no where fast:
<a href="@Uri.EscapeDataString("https://www.notmysite.co.uk/controller/action?order_ID=" + Model.bookingNumber + "&hashComparator=" + Model.hashCode)">Click Here to be transferred</a>
The output text always has plusses and equals in them and doesn't work. Which combination do I need?!
I've figured out a way of doing it:
@{
var url = string.Format(
"https://www.notmysite.co.uk/controller/action?order_ID={0}&hashComparator={1}",
@Uri.EscapeDataString(Model.bookingNumber.ToString()),
@Uri.EscapeDataString(Model.hashCode));
}
<p><a href="@url">Click Here to be transferred</a></p>
Edit 2015 - As mentioned by Jerads post - The solution is to only encode the query string elements and not the whole URL - which is what the above does.