加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 将CurrentUICulture传递给ASP.NET MVC 3.0中的异

发布时间:2020-12-16 07:44:45 所属栏目:asp.Net 来源:网络整理
导读:活动语言是从URL确定的,然后设置在 Thread.CurrentThread.CurrentUICulture = cultureInfo;Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name); 这样,从正确的资源文件中检索翻译. 在控制器上使用Async操作时,我们
活动语言是从URL确定的,然后设置在

Thread.CurrentThread.CurrentUICulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);

这样,从正确的资源文件中检索翻译.

在控制器上使用Async操作时,我们有一个后台线程,其中Thread.CurrentThread.CurrentUICulture设置回操作系统默认值.但是在后台线程中我们需要正确的语言.

我创建了一个TaskFactory扩展来将文化传递给后台线程,它看起来像这样:

public static Task StartNew(this TaskFactory taskFactory,Action action,CultureInfo cultureInfo)
{
    return taskFactory.StartNew(() =>
    {
         Thread.CurrentThread.CurrentUICulture = cultureInfo;
         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);

         action.Invoke();
     });
}

这允许我在动作控制器中执行以下操作:

[HttpPost]
 public void SearchAsync(ViewModel viewModel)
 {
     AsyncManager.OutstandingOperations.Increment();
     AsyncManager.Parameters["task"] = Task.Factory.StartNew(() =>
     {
         try
         {
               //Do Stuff
               AsyncManager.Parameters["viewModel"] = viewModel;
         }
         catch (Exception e)
         {
             ModelState.AddModelError(string.Empty,ResxErrors.TechnicalErrorMessage);
         }
         finally
         {
             AsyncManager.OutstandingOperations.Decrement();
         }
     },Thread.CurrentThread.CurrentUICulture);
 }



 public ActionResult SearchCompleted(Task task,ViewModel viewModel)
 {
     //Wait for the main parent task to complete. Mainly to catch all exceptions.
     try { task.Wait(); }
     catch (AggregateException ae) { throw ae.InnerException; }

     return View(viewModel);
 }

这一切都很完美,但我确实有一些顾虑.

这是通过在调用原始操作之前设置文化来扩展操作的正确方法吗?

有没有人知道将te CurrentUICulture传递给ASP.NET MVC异步操作的后台线程的不同方法?

>会话不是一种选择.
>我确实在考虑使用CallContext.

对此代码有何评论?

谢谢

解决方法

看来问题中描述的方式就是答案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读