URL Encode string for Href ASP.NET MVC / Razor

前端 未结 4 982
长发绾君心
长发绾君心 2020-12-16 12:34

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

4条回答
  •  -上瘾入骨i
    2020-12-16 12:44

    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));
    }
     

    Click Here to be transferred

    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.

提交回复
热议问题