URL恩code字符串HREF ASP.NET MVC /剃刀 [英] URL Encode string for Href ASP.NET MVC / Razor
问题描述
我试图建立使用剃刀一个href
该字符串将要结束这样看:
I'm trying to build an Href using Razor The string is going to end up looking like this:
<一个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
在我的模型我有订单ID和散列串
由于路由不是我的网站的一部分,我不相信我可以使用默认的方法,如@ Url.Action
因此不能使用协议:Request.Url.Scheme
就像我在别处使用。
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.
因此,在present我试图找出如何创造这个使用字符串函数
我试过了
Url.En code
Url.EscapeDataString
Html.En code
但我没有得到快速的地方:
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>
2015年编辑 - 正如Jerads文章中提到 - 解决方案是唯一的EN code中的查询字符串的元素,而不是整个URL - 这正是上面确实
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.
这篇关于URL恩code字符串HREF ASP.NET MVC /剃刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!