【发布时间】:2021-07-23 15:57:44
【问题描述】:
我正在尝试使用以下代码生成 url 字符串:
var x = @Url.Action("AddOrEditSaleOffer", "BuildingOffers", null);
但是,返回的值会以某种方式生成以下 url:
/BuildingOffers/AddOrEditSaleOffer/7
其中 7 是一个数字,它会根据上次编辑的商品的 ID 而变化。
代码在局部视图中,使用 ajax 调用并在对 Building Offer 记录进行编辑后调用。我希望得到 NO id,但 url 以某种方式附加了一个 id。
更新:
我发现以下问题有助于理解我为什么会遇到这个问题,并建议了解决方法:Prevent ambient route values from the URL from being added to Html.Action and Html.ActionLinks automatically in ASP.NET
此外,Microsoft Docs 中的以下部分。讨论这个问题:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-5.0#url-generation-reference
【问题讨论】:
-
如果省略
null参数会怎样? -
我得到的结果与 null 存在时相同。 (id 还是通过了)。
-
奇怪的是它在第一次生成时可以正常工作,但是当我在编辑记录后使用 ajax 调用该页面时,它传递了我刚刚更新的记录的 id。
-
为什么不只使用 var x = " /BuildingOffers/AddOrEditSaleOffer" ???
-
@Sergey 使用
Url.Action被认为更健壮。路由模式可能会改变,然后字符串会中断,而Url.Action会生成正确的 url。
标签: c# html asp.net asp.net-mvc asp.net-core