Url.Action将一个&在我的url中,我该如何解决这个问题

Url.Action puts an & in my url, how can I solve this?

本文关键字:amp 问题 何解决 解决 Action 一个 url 我的 Url      更新时间:2023-09-26

我想将变量itemId和entityModel发送到ActionResult CreateNote:

public ActionResult CreateNote(
        [ModelBinder(typeof(Models.JsonModelBinder))]
        NoteModel Model, string cmd, long? itemId, string modelEntity)

使用以下javascript:

Model.meta.PostAction = Url.Action("CreateNote", new { cmd = "Save", itemId = itemId, modelEntity = modelEntity});

但是,正在发送的url是

localhost:1304/Administration/blue/en-gb/Entity/CreateNote?modelEntity=Phrase&itemId=44     

我想发送

localhost:1304/Administration/blue/en-gb/Entity/CreateNote?modelEntity=Phrase&itemId=44

如何阻止Url.Action将&在我要发送的第二个变量前面?

我昨天没有注意到你有&,我以为是SO编辑器更改了。尝试将Url.Action()包装在@Html.Raw()中,以防止&的Encode;。

或者,只Url.Action()控制器/操作位,并将这两个参数作为post数据而不是直接在url上传递,jQuery应该对&amp那是给你的。

我认为您的问题是Model.meta.PostAction-该属性是string吗?

如果是这样的话,那么我的猜测是你将其添加到页面中,并使用以下任一选项:

  • 剃刀:@Model.meta.PostAction
  • ASP视图引擎:<%:Model.meta.PostAction%>

这两者都会自动为您对字符串进行编码。

要修复它,可以使用@Html.Raw()/<%=(两者都不编码),也可以将PostAction属性设置为知道它已经编码的IHtmlString

string actionUrl = Url.Action("CreateNote", new { cmd = "Save", itemId = itemId, modelEntity = modelEntity});
Model.meta.PostAction = new HtmlString(actionUrl);