is there a way to Change the Current url from Map Route to another Map Route?

88 Views Asked by At

i have two Map Route :

1- "{controller=Home}/{action=Index}/{id?}");

2-"{culture=en}/{controller=Home}/{action=Index}/{id?}"

all i want is there is a way to Convert link : DomainName.com/Login/Register (Map Route 1) to DomainName.com/en/Login/Register (Map Route 2)

without Lose Action,Controller or id

2

There are 2 best solutions below

3
Y Stroli On

You can split the URL string by'/' character and enter culture at index[1]:

string UrlWithCulture(string url, string culture)
{
  string[] split = url.Split("/");
  return $"{split[0]}/{culture}/{string.Join("/",split.Skip(1))}";
}
0
Ayman Maraghy On

i did it by JS

$(document).ready(function () {
var url = window.location.href;
var segments = url.split('/');
console.log(url);
console.log(segments);
if (segments.includes("en")) {
    segments.splice(3, 1, "ar");
    var newAr = segments.join("/");
    console.log(newAr);
    $(".changeAr").click(function () {
        window.location.replace(newAr);

    });
} else if (segments.includes("ar")) {
    segments.splice(3, 1, "en");
    var newEn = segments.join("/");
    console.log(newEn);
    $(".changeEn").click(function () {
        window.location.replace(newEn);
        console.log(newEn);
    });
} else {
    segments.splice(3, 0, "en");
    var newEn = segments.join("/");
    console.log(newEn);
    $(".changeEn").click(function () {
        window.location.replace(newEn);
    });
    segments.splice(3, 1, "ar");
    var newAr = segments.join("/");
    console.log(newAr);
    $(".changeAr").click(function () {
        window.location.replace(newAr);
    });
}