asp.net-mvc MVC3 Url.Action 查询字符串生成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5872157/
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 01:10:05  来源:igfitidea点击:

MVC3 Url.Action querystring generation

asp.net-mvcasp.net-mvc-3urlrazorquery-string

提问by egyedg

I am trying to generate an url for an MVC 3 action within javascript environment (in a cshtml file).

我正在尝试在 javascript 环境中(在 cshtml 文件中)为 MVC 3 操作生成一个 url。

<script type="text/javascript">
  ...
  var src = "@Url.Action("GetProductImage", new { productId = Model.Product.Id, pos = 1, size = 0 })";
  $(document.createElement("img")).attr("src", src);
  ...
</script>

Now this works almost fine, my problem is that the querystring is being escaped. Instead of:

现在这几乎可以正常工作,我的问题是查询字符串正在被转义。代替:

"/Products/GetProductImage?productId=1&pos=0&size=0"

it generates:

它产生:

"/Products/GetProductImage?productId=1&amp;pos=0&amp;size=0"

so my action does not get called.

所以我的行动不会被调用。

Now I know I can make my own custom Url helper function, but I was wondering if I can use this or some other built in helper to get the unescaped URL?

现在我知道我可以创建自己的自定义 Url 辅助函数,但我想知道是否可以使用这个或其他一些内置的辅助函数来获取未转义的 URL?

Thanks in advance, G.

提前致谢,G。

回答by Govind Malviya

<script type="text/javascript">
   var src = "@Html.Raw(Url.Action("GetProductImage", new { productId = Model.Product.Id, pos = 1, size = 0 }))";
   $(document.createElement("img")).attr("src", src);
</script>

回答by Har

var src = "@Html.Raw(Url.Action("GetProductImage", new { productId = Model.Product.Id, pos = 1, size = 0 }))";

Url.Action worked for me notHtmlUrl.Action

Url.Action 对我有用,而不是HtmlUrl.Action

Enjoy!

享受!