繁体   English   中英

如何在 MVC 上替换 url 中的参数

[英]How can i replace parameter from url on MVC

我喜欢

https://localhost:12345/en/Dashboard

我有@item.code="gr" ,我想在这里用'gr'替换'en'我怎么能在html中做到这一点

<a href="@Url.Path.Replace($"/{currentlangugage.code}", $"/{item.code}")>

什么是正确的,以及 cshtml 文件中的这段代码。 我怎样才能在我的视图上轻松做到这一点? 谢谢你帮助我!

如果您坚持要替换 URL 的一部分,您应该考虑使用正则表达式来专门标识 URL 路径开头的当前语言代码。

把它贴在你的视野中的某个地方

@functions{
    public string ReplaceLang(string path, string currentCode, string newCode) {
        var langRegex = new Regex($"(?<=^/){currentCode}");

        return path.Replace(path, newCode);
    }
}

然后像这样使用它

<a href="@ReplaceLang(Url.Path, currentlangugage.code, item.code)")>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM