有没有办法将当前网址从“地图路线”更改为另一个“地图路线”?

时间:2019-08-30 00:03:21

标签: c# asp.net-core model-view-controller maproute

我有两条地图路线:

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

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

我所希望的是有一种转换链接的方法:DomainName.com/Login/Register(映射路线1) 至 DomainName.com/zh-CN/Login/Register(映射路线2)

没有丢失动作,控制器或ID

2 个答案:

答案 0 :(得分:0)

我是用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);
    });
}

答案 1 :(得分:-1)

您可以将URL字符串用'/'字符分割,然后在索引[1]处输入区域性:

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