25

I'm trying to build an Href using Razor The string is going to end up looking like this:

https://www.notmysite/controller/action?order_ID=xxxxxxx&hashComparator=iFxp3%2BszAMaEB%2FnHCtRr9Ulhv0DumtyDumCik4gKypJqi0BdOGXXsr9QDkfefAsLaR1Xy%2BrX9VcuzP1pF2k6dL%2F92UxphzTKqNAZP2SSZGWtvyO5az%2F9JvDY%2Bsq5TBQo7%2FYAAMIU4QxiSX1SBKk8SUNECW3ZmKM%3D

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?!

Chris Nevill
  • 5,922
  • 11
  • 44
  • 79

4 Answers4

48

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.

Feanaro
  • 922
  • 3
  • 19
  • 35
Chris Nevill
  • 5,922
  • 11
  • 44
  • 79
5

This was the first link that came up for this issue for me. The answers didn't work for me though because I am using core, I think. So wanted to add this in.

System.Net.WebUtility.UrlEncode(MyVariableName)

If Url.Encode doesn't work try the above. Also as stated before don't use this on the entire URL string, just use it for the individual querystring variables. Otherwise there is a good chance your URL wont work.

Deathstalker
  • 794
  • 10
  • 8
4

The problem is that you're trying to encode the whole URL. The only pieces you want to encode are the querystring values, and you can just use Url.Encode() for this.

You don't want to encode the address, the querystring params, or the ? and & delimiters, otherwise you'll end up with an address the browser can't parse.

Ultimately, it would look something like this:

<a href="https://www.notmysite.co.uk/controller/action?order_ID=@Url.Encode(Model.bookingNumber)&hashComparator=@Url.Encode(Model.hashCode)">Click Here to be transferred</a>
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
  • I'm not really sure that your answer adds anything over mine other than that it's all on one line... Url.Encode and Uri.EscapeDataString seem to be two sides of the same coin http://blog.nerdbank.net/2009/05/uriescapedatapath-and.html I guess you've got an explanation of what's happening.. but that could have been a comment? – Chris Nevill Oct 21 '15 at 20:56
  • There are [some differences](http://blog.nerdbank.net/2009/05/uriescapedatapath-and.html), but the important part of my clarification, aside from the inlining, is that you didn't mention that you were trying to encode the full URL, and not just the bits that needed encoding. – Jerad Rose Oct 22 '15 at 16:26
  • That is what my answer does though - it does not encode the full URL. I just don't see your answer as a new answer. Just my answer with better sugar coating. – Chris Nevill Oct 22 '15 at 16:32
  • As a side note you've linked to the same article as me in that comment. I did read it. If anything it sounds like EscapeDataString is more RFC 3986 compliant... but in a very minimal way... I don't think that will affect the vast majority of people here. – Chris Nevill Oct 22 '15 at 16:40
  • 2
    This is a good tip for encoding only *parts* of a URL, which is sometimes necessary and preferable over encoding it in its entirety. +1 – InteXX Sep 17 '16 at 00:04
-5

The easier method is to use @Html.Raw(Model.SomethingUrl)

Andrew Harry
  • 13,773
  • 18
  • 67
  • 102