1

我有一个 MVC4 应用程序,它在路由/url 中有一个额外的元素,它被命名为语言:

routes.MapRoute(
    name: "languageDefault",
    url: "{language}/{controller}/{action}/{id}",
    defaults: new {
        language = "en-US",
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional
    }
);

现在,每次执行操作时,我都会使用自定义 filterAttribute 检查语言是否设置正确:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    string cultureName = null;
    var request = filterContext.HttpContext.Request;
    string currentUrl = request.RawUrl;
    string[] splittedStrings = currentUrl.Split('/');

    string currentLanguage  = CultureHelper.CheckCulture();
    var cultureCookie       = request.Cookies["_culture"];
    var possibleCultures    = UnitOfWork.CulturesRepository.GetListOfCultureNames();

    foreach (string culture in possibleCultures)
    {
        if (splittedStrings[1] == culture) cultureName = splittedStrings[1];
    }
    if (cultureCookie != null) cultureName = cultureCookie.Value;
    if (currentLanguage != null) cultureName = currentLanguage;

    if (cultureName != null)
    {
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
    }
    else
    {
        cultureName = possibleCultures[0];
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
    }

    base.OnActionExecuting(filterContext);
}

我现在的问题是,当我检查是否存在语言时,如果没有,设置一个,如果提供的语言不正确,我真的什么都不做。例如当用户键入:

http://www.example.com/en-US/shop

他提供了正确的网址,因此将正确地看到商店页面。但是,如果他不提供这样的语言:

http://www.example.com/shop

语言检查完成,根据他是否是第一次访问,设置他的语言。问题在于url 没有改变

我希望更改 url,以便它显示用户查看页面的语言。换句话说,当试图访问

http://www.example.com/shop

他应该被重定向到:

http://www.example.com/en-US/shop

无论访问者访问哪个页面,这都需要发生。换句话说,我可能需要在 global.asax 或我提供的覆盖函数中添加一些东西。但我不知道是什么以及如何。

4

1 回答 1

0

我通过改变我的 OnActionExecuting 方法解决了这个问题:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var request         = filterContext.HttpContext.Request;
        string currentUrl   = request.RawUrl;
        var urlHelper       = new UrlHelper(request.RequestContext);

        string baseurl                      = urlHelper.Content("~");
        string currentLanguageFromUrl       = currentUrl.Split('/')[1];
        string currentLanguageFromCulture   = CultureHelper.CheckCulture();
        var currentLanguageFromCookie       = request.Cookies["_culture"];

        var possibleCultures                = UnitOfWork.CulturesRepository.GetListOfCultureNames();

        if (possibleCultures.All(culture => currentLanguageFromUrl != culture))
        {
            string cultureName;
            string newUrl;

            if (currentLanguageFromCookie != null)
            {
                cultureName = currentLanguageFromCookie.Value;
                CultureHelper.SetCulture(cultureName);
                newUrl = baseurl + cultureName;
                filterContext.Result = new RedirectResult(newUrl);
                return;
            }

            if (currentLanguageFromCulture != null)
            {
                cultureName = currentLanguageFromCulture;
                CultureHelper.SetCulture(cultureName);
                newUrl = baseurl + cultureName;
                filterContext.Result = new RedirectResult(newUrl);
                return;
            }

            cultureName = possibleCultures[0];
            CultureHelper.SetCulture(cultureName);
            newUrl = baseurl + cultureName;
            filterContext.Result = new RedirectResult(newUrl);
            return;
        }

        base.OnActionExecuting(filterContext);
    }

现在,当您填写不正确的语言时,您将被重定向到具有正确语言的页面,如果您提供两个不正确的参数,您将被重定向到 404(这是合乎逻辑的)。

于 2013-02-03T20:50:21.297 回答