Question

I am generating an URL in JavaSctipt. The first time I generate this URL I get the correct URL: E.g. **http://localhost:54415/NffCall/Details/DK/6607726**

But the next time I generate the link I get the previous link combined with current parameters: E.g. http://localhost:54415/NffCall/Details/DK/6607726/DK/6608146

The code which genrates my URLs looks like this:

var actionUrlBase = '@Url.Action("Details", "NffCall")';
var actionUrl = actionUrlBase + '/' + countryCode + '/' + orderNumber;
window.location.href = actionUrl;

What shall I do to generate the correct link every time?

EDIT: I will just paste how the RouteCollection looks like:

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 }
);
Was it helpful?

Solution

Use the Helper @Url.RouteUrl instead.

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

This site explains it very well: http://www.patridgedev.com/2011/08/22/subtleties-with-using-url-routeurl-to-get-fully-qualified-urls/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top