我使用此代碼進行全球化併爲新用戶設置語言cookie,並且這是基類BaseController
。但它根本沒有執行。請幫幫我。基礎控制器爲未使用的新用戶設置語言cookie
using System;
using System.Web.Mvc;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Threading;
using System.Globalization;
namespace App1.Models
{
public class BaseController : Controller
{
private const string LanguageCookieName = "cookie_lang";
protected override void ExecuteCore()
{
var cookie = HttpContext.Request.Cookies[LanguageCookieName];
string lang;
lang = ConfigurationManager.AppSettings["DefaultCulture"] ?? "en-US";
if (cookie != null)
{
lang = cookie.Value;
}
else
{
var httpCookie = new HttpCookie(LanguageCookieName, lang)
{
Expires = DateTime.Now.AddYears(1)
};
HttpContext.Response.SetCookie(httpCookie);
}
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
base.ExecuteCore();
}
}
}
不錯的一個。清晰簡單的文章。謝謝 – Beaumind
tnx @ mohammad-aliyan – msitworld