首页 > 解决方案 > Url.Action 方法返回错误的 url 字符串

问题描述

我正在尝试使用以下代码生成 url 字符串:

var x = @Url.Action("AddOrEditSaleOffer", "BuildingOffers", null);

但是,返回的值会以某种方式生成以下 url:

/BuildingOffers/AddOrEditSaleOffer/7

其中 7 是根据上次编辑的商品的 ID 更改的数字。

该代码位于部分视图中,并在对 Building Offer 记录进行编辑后使用 ajax 调用进行调用。我希望得到 NO id,但 url 以某种方式附加了一个 id。

更新:

我发现以下问题有助于理解我遇到此问题的原因并建议解决方法:防止 URL 中的环境路由值自动添加到 ASP.NET 中的 Html.Action 和 Html.ActionLinks

此外,Microsoft Docs 中的以下部分。讨论这个问题:https ://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-5.0#url-generation-reference

标签: c#htmlasp.netasp.net-mvcasp.net-core

解决方案


If you don't want to send parameters in your url, you can use the other Action method overload, which takes controller and action as input parameters.

var url = @Url.Action("AddOrEditSaleOffer", "BuildingOffers");

推荐阅读