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异步操作的后台线程的不同方法? >会话不是一种选择. 对此代码有何评论? 谢谢 解决方法
看来问题中描述的方式就是答案.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 我应该把我的控制器业务逻辑放在MVC3中
- 如何在asp.net中使用facebook api创建一个facebook事件
- asp.net-mvc – 如何单元测试返回ContentResult的ActionRes
- asp.net-mvc – 如何使用Data Annotations Extensions在属性
- asp.net-mvc – 模型本身应该进行一些计算吗?
- asp.net – 使用CompareValidator控件将用户输入日期与今天
- asp.net-mvc – 多个MVC项目结构
- asp.net – 如何关闭我在JavaScript中打开的所有IE浏览器窗
- asp.net – 您在哪里存储数据库连接字符串?
- asp.net-mvc – ASP.NET MVC:将会话状态保存在类似向导的应