Url.Action将先前的URL与VS IDE中的JavaScript中的当前路由参数组合在一起

时间:2013-11-06 14:04:58

标签: javascript asp.net-mvc routes url.action

我在JavaSctipt中生成一个URL。 我第一次生成此URL时,我得到了正确的URL: 例如。 **http://localhost:54415/NffCall/Details/DK/6607726**

但是下次我生成链接时,我得到了前一个链接和当前参数: 例如。 http://localhost:54415/NffCall/Details/DK/6607726/DK/6608146

生成我的网址的代码如下所示:

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 }
);

1 个答案:

答案 0 :(得分: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/