2

我在 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 }
);
4

1 回答 1

2

请改用 Helper @Url.RouteUrl。

var actionUrlBase = '@Url.RouteUrl("Default",new {action="Details", controller= "NffCall"},Request.Url.Scheme)';
var actionUrl = actionUrlBase + '/' + countryCode + '/' + orderNumber;
window.location.href = actionUrl;

这个网站解释得很好: http: //www.patridgedev.com/2011/08/22/subtleties-with-using-url-routeurl-to-get-fully-qualified-urls/

于 2013-11-06T14:30:58.200 回答