Home > database >  How can i change on MVC
How can i change on MVC

Time:09-27

i have like

https://localhost:12345/en/Dashboard

and i have @item.code="gr" and I want to replace in here 'en' with 'gr' how can i do that in html

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

What is the correct one and this code in cshtml file. How can i do that easily on my View? Thanks for helping me!

CodePudding user response:

If you insist on replacing part of the URL you should think about using a regular expression to identify specifically the current language code at the beginning of the URL's path.

Stick this in your view somewhere

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

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

Then use it something like this

<a href="@ReplaceLang(Context.Request.Path, currentlangugage.code, item.code)")>
  • Related