Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么RedirectToAction会更改ThreadID?_C#_Asp.net Mvc - Fatal编程技术网

C# 为什么RedirectToAction会更改ThreadID?

C# 为什么RedirectToAction会更改ThreadID?,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个web应用程序(ASP.NET MVC 3),支持en US、pt BR、es的本地化 public class HomeController : Controller { public ActionResult Index() { int threadId = Thread.CurrentThread.ManagedThreadId; string threadUICulture = Thread.CurrentThread.CurrentUICulture.T

我有一个web应用程序(ASP.NET MVC 3),支持en US、pt BR、es的本地化

public class HomeController : Controller
{
  public ActionResult Index()
  {
    int threadId = Thread.CurrentThread.ManagedThreadId;
    string threadUICulture = Thread.CurrentThread.CurrentUICulture.ToString();
    return View();
  }
}

public class LanguageController : Controller
{
  public ActionResult EN()
  {
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    return RedirectToAction("Index", "Home");
  }

  public ActionResult ES()
  {
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES");
    Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");

    int threadId = Thread.CurrentThread.ManagedThreadId;
    return RedirectToAction("Index", "Home");
  }

  public ActionResult PT()
  {
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-BR");
    Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR");
    return RedirectToAction("Index", "Home");
  }
}
我使用culture en US启动应用程序,使用ActionLink,我调用语言/ES以更改为西班牙语并重定向到索引页。但是不工作,索引页仍然是英文的

我发现方法语言controller.ES和HomeController.Index上的线程Id不同


在ASP.NET MVC应用程序上管理本地化的最佳方法是什么?

我认为,在您的控制器中,设置会话文化应该有效

会话[“文化”]=新文化信息(“es”)


HttpContext.Session[“culture”]=新的CultureInfo(“es”)

发生这种情况是因为每个请求都是从线程池中提取的线程提供的。因此,虽然第一个请求可以在线程A上提供服务,但第二个请求可以在线程B上提供服务。您需要在某个地方保存有关当前区域性的信息。这可能是:会话、cookies、一些url路由参数等。请看下面的内容。

这里有一篇文章可能会让您朝着正确的方向前进。每个请求都由不同的线程处理。