我正在 JavaSctipt 中生成一个 URL。
第一次生成此 URL 时,我得到了正确的 URL:
例如。 **http://localhost:54415/NffCall/Details/DK/6607726**
但是下次生成链接时,我会得到前一个链接与当前参数的组合:
例如。 http://localhost:54415/NffCall/Details/DK/6607726/DK/6608146
生成我的 URL 的代码如下所示:
var actionUrlBase = '@Url.Action("Details", "NffCall")';
var actionUrl = actionUrlBase + '/' + countryCode + '/' + orderNumber;
window.location.href = actionUrl;
我该怎么做才能每次生成正确的链接?
编辑: 我将粘贴 RouteCollection 的样子:
routes.MapRoute(
name: "CountryCodeOrderNumberDetails",
url: "{controller}/{action}/{countryCode}/{orderNumber}",
defaults: new { controller = "NffCall", action = "Details", countryCode = (string)null, orderNumber = (int?)null },
constraints: new { countryCode = "[a-zA-Z]{2}", orderNumber = "[0-9]+" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
最佳答案
改用助手@Url.RouteUrl。
var actionUrlBase = '@Url.RouteUrl("Default",new {action="Details", controller= "NffCall"},Request.Url.Scheme)';
var actionUrl = actionUrlBase + '/' + countryCode + '/' + orderNumber;
window.location.href = actionUrl;
关于javascript - Url.Action 在 VS IDE 中的 JavaScript 中将先前的 URL 与当前的路由参数结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19814160/