ASP.NET 核心 MVC 和 C# - 使用区域性更改语言时重定向到同一视图(操作)

ASP.NET Core MVC & C# - redirect to same view ( action ) when changing language using culture

我的项目中有本地化。我想要的是每当用户更改语言时,它都会将他重定向到他所在的同一页面。但现在我总是将他重定向到 Index 页面。

视图(布局):

<a asp-controller="language" asp-action="change" asp-route-culture="en-GB" class="btn btn-clear primary"›
<i class="fas fa-globe-americas mx-1" title="Arabic"></i>@sharedResource["arabic"]
</a›
‹a asp-controller="language" asp-action="change" asp-route-culture="en-US" class="btn btn-clear-primary"›
<i class-"fas fa-globe-americas mx-1" title="English"›</i>English
</a›

控制器(语言控制器):

public IActionResult Change( string culture)
{
    Response.Cookies.Append (CookieRequestCultureProvider.DefaultCookieName,
                             CookieRequestCultureProvider.MakeCookieValue(newRequestCulture(culture)),
    new CookieOptions ( Expires = DateTimeOffset.UtcNow.AddMonths(1)}
):

    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
    Thread.CurrentThread.CurrentUICulture=CultureInfo.CreateSpecificCulture(culture);
    //But independent of language, keep datetime format same
    DateTimeFormatInfo englishDateTimeFormat = new CultureInfo("en-CA").DateTimeFormat;
    Thread.CurrentThread.CurrentCulture.DateTimeFormat=englishDateTimeFormat;

    return RedirectToAction("Index", "Home");
}

如何将用户重定向到他更改其中语言的同一页面?

我这样做并且对我有用。

public IActionResult Change(string culture)
{
    Response.Cookies.Append (CookieRequestCultureProvider.DefaultCookieName,
                             CookieRequestCultureProvider.MakeCookieValue(newRequestCulture(culture)),
    new CookieOptions ( Expires = DateTimeOffset.UtcNow.AddMonths(1)}
);

    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
    Thread.CurrentThread.CurrentUICulture=CultureInfo.CreateSpecificCulture(culture);
    //But independent of language, keep datetime format same
    DateTimeFormatInfo englishDateTimeFormat = new CultureInfo("en-CA").DateTimeFormat;
    Thread.CurrentThread.CurrentCulture.DateTimeFormat=englishDateTimeFormat;

string referer = Request.Headers["Referer"].ToString();
string action = referer.Substring(referer.LastIndexOf("/")+1,referer.Length-referer.LastIndexOf("/")-1);
string afterRemoveAction = referer.Substring(0, referer.LastIndexof("/") );
string controller = afterRemoveAction.Substring(afterRemoveAction.LastIndexOf("/")+1,
afterRemoveAction.Length-afterRemoveAction.LastIndexOf("/")- 1);
return RedirectToAction(action, controller);
}