asp.net-mvc Href ASP.NET MVC / Razor 的 URL 编码字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21159382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:26:46  来源:igfitidea点击:

URL Encode string for Href ASP.NET MVC / Razor

asp.net-mvcrazorurlencode

提问by Chris Nevill

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

我正在尝试使用 Razor 构建一个 Href 字符串最终看起来像这样:

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

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.

在我的模型中,我有订单 ID 和哈希字符串 由于路由不是我网站的一部分,我不相信我可以使用像 @Url.Action 这样的默认方法,因此不能使用协议:Request.Url。就像我在别处使用过的方案。

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:

所以目前我正试图弄清楚如何使用字符串函数来创建它,我已经尝试过 Url.Encode Url.EscapeDataString Html.Encode 但我没有任何速度:

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

输出文本总是有加号和等号,并且不起作用。我需要哪种组合?!

回答by Chris Nevill

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.

编辑 2015 - 正如 Jerads 帖子所提到的 - 解决方案是只编码查询字符串元素而不是整个 URL - 这就是上面所做的。

回答by Jerad Rose

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.

问题是您正在尝试对整个 URL 进行编码。您想要编码的唯一部分是 querystring values,您可以使用Url.Encode()它。

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/[email protected](Model.bookingNumber)&[email protected](Model.hashCode)">Click Here to be transferred</a>

回答by Deathstalker

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.

如果 Url.Encode 不起作用,请尝试以上操作。同样如前所述,不要在整个 URL 字符串上使用它,只需将它用于单个查询字符串变量。否则,您的 URL 很有可能无法工作。

回答by Andrew Harry

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

更简单的方法是使用@Html.Raw(Model.SomethingUrl)