由於公司慢慢地開始將新的專案都移往 Windows Azure 雲端平台做網站代管,漸漸地也開始遇到一些小問題,這些問題在還沒上雲端之前通常不會發生,像我們這次遇到的問題就跟顯示時間有關。由於 Windows Azure 雲端平台是全球性、跨國家、跨地域的服務平台,因此,無論是 .NET 執行環境的時區,或是 SQL Database 資料庫的時區,預設都是以UTC 世界標準時間 (Coordinated Universal Time) 為主,也因此在上面執行的網站在顯示時間時,你就會覺得時間好像怪怪的,怎麼都不準。今天我就是要來講怎樣利用 ASP.NET MVC 的 範本檢視(Templated View) 來有效的解決這個問題。

首先,在使用 Azure WebSite 或 Azure Cloud Service 來執行網站時,由於你幾乎無法碰觸到執行環境的任何設定,因此變更系統時區變得不太可能,除非你直接租用 Azure Virtual Machine 才有機會,不過這種執行方式無法提升網站的延展性 (Scalability),且成本也高,因此不太建議直接租用 VM 來執行網站。

也因為你無法修改系統的時區設定,所以,要解決這個方法,只有兩種可能:

  • 要儲存到資料庫或其他地方時,先將本地時間轉換成 UTC +8 的時間 (台北標準時間)
  • 顯示資料時,再轉換成 UTC +8 的時間 (台北標準時間)

我個人是比較偏好儲存資料時一律使用 UTC 標準時間來儲存,以確保時間的一致性。然後顯示時間時,再利用 .NET 內建的轉換類別進行時區轉換,已顯示適當的本地時間。

: 以下關於 範本檢視 的基本知識,建議可參考 ASP.NET MVC 4 開發實戰 第7章 View 相關技術。

由於我們在 ASP.NET MVC 裡面,大多是使用以下 Html 範本輔助方法來輸出各種時間:

@Html.DisplayFor(m => m.CreatedOn)

因此,要解決時間顯示的問題,解決的方法非常簡單,只要在 Views\Shared\DisplayTemplates 目錄下新增一個 DateTime.cshtml 檔案,如下圖示:

其 DateTime.cshtml 程式碼內容如下,註解都寫在程式中,各位可以自行到 MSDN 查閱相關說明:

@model DateTime?
@{
if (Model.HasValue)
{
DateTime dtCurrentTime; // 判斷本機的時區設定是否為 UTC 時間,如果是,就要進行轉換,否則就直接顯示本地時間
if (TimeZoneInfo.Local == TimeZoneInfo.Utc)
{
// 以下是取得 "台北標準時區" 的標準寫法
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Taipei Standard Time"); // 依據取得的時區進行時間轉換
dtCurrentTime = TimeZoneInfo.ConvertTime(Model.Value, tzi);
}
else
{
dtCurrentTime = Model.Value;
} @dtCurrentTime.ToString()
}
}

在 ASP.NET MVC 裡面,就這樣簡簡單單的新增一個檔案,整個網站的顯示時間就會全部都自動修正完畢,這也是我經常在課堂上講的【關注點分離】之美!  (^_^)

課程公告[台北市] 招生中,6/9(週日) Will 保哥 ASP.NET MVC 4...六週 開發實戰

§ 備註

如果想得知全球各時區的 id,建議可參考 TimeZoneInfo.GetSystemTimeZones Method 文件,裡面有範例程式可取得所有系統內建的 id 清單,如此一來你就可以利用 TimeZoneInfo.FindSystemTimeZoneById來取得特定地點的時區資訊。

為了方便各位查找,我已經取出所有時區的 id 清單,如有需要的也可以直接剪貼取用:

  1. Dateline Standard Time
  2. UTC-11Hawaiian Standard Time
  3. Alaskan Standard Time
  4. Pacific Standard Time (Mexico)
  5. Pacific Standard Time
  6. US Mountain Standard Time
  7. Mountain Standard Time
  8. Mountain Standard Time (Mexico)
  9. Central America Standard Time
  10. Central Standard Time
  11. Central Standard Time (Mexico)
  12. Canada Central Standard Time
  13. US Eastern Standard Time
  14. Eastern Standard Time
  15. SA Pacific Standard Time
  16. Venezuela Standard Time
  17. Paraguay Standard Time
  18. SA Western Standard Time
  19. Central Brazilian Standard Time
  20. Atlantic Standard Time
  21. Pacific SA Standard Time
  22. Newfoundland Standard Time
  23. E. South America Standard Time
  24. Argentina Standard Time
  25. Greenland Standard Time
  26. Montevideo Standard Time
  27. Bahia Standard Time
  28. SA Eastern Standard Time
  29. UTC-02Mid-Atlantic Standard Time
  30. Azores Standard Time
  31. Cape Verde Standard Time
  32. Morocco Standard Time
  33. UTCGreenwich Standard Time
  34. GMT Standard Time
  35. W. Central Africa Standard Time
  36. Central European Standard Time
  37. Romance Standard Time
  38. Namibia Standard Time
  39. Central Europe Standard Time
  40. W. Europe Standard Time
  41. Turkey Standard Time
  42. South Africa Standard Time
  43. Syria Standard Time
  44. E. Europe Standard Time
  45. Israel Standard Time
  46. Middle East Standard Time
  47. FLE Standard Time
  48. Egypt Standard Time
  49. GTB Standard Time
  50. Kaliningrad Standard Time
  51. E. Africa Standard Time
  52. Jordan Standard Time
  53. Arabic Standard Time
  54. Arab Standard Time
  55. Iran Standard Time
  56. Azerbaijan Standard Time
  57. Georgian Standard Time
  58. Russian Standard Time
  59. Caucasus Standard Time
  60. Mauritius Standard Time
  61. Arabian Standard Time
  62. Afghanistan Standard Time
  63. Pakistan Standard Time
  64. West Asia Standard Time
  65. Sri Lanka Standard Time
  66. India Standard Time
  67. Nepal Standard Time
  68. Ekaterinburg Standard Time
  69. Bangladesh Standard Time
  70. Central Asia Standard Time
  71. Myanmar Standard Time
  72. SE Asia Standard Time
  73. N. Central Asia Standard Time
  74. W. Australia Standard Time
  75. North Asia Standard Time
  76. China Standard Time
  77. Taipei Standard Time
  78. Singapore Standard Time
  79. Ulaanbaatar Standard Time
  80. North Asia East Standard Time
  81. Tokyo Standard Time
  82. Korea Standard Time
  83. Cen. Australia Standard Time
  84. AUS Central Standard Time
  85. Yakutsk Standard Time
  86. AUS Eastern Standard Time
  87. E. Australia Standard Time
  88. West Pacific Standard Time
  89. Tasmania Standard Time
  90. Vladivostok Standard Time
  91. Central Pacific Standard Time
  92. UTC+12New Zealand Standard Time
  93. Kamchatka Standard Time
  94. Fiji Standard Time
  95. Magadan Standard Time
  96. Tonga Standard Time
  97. Samoa Standard Time

http://blog.miniasp.com/post/2013/04/10/How-to-convert-Times-between-Time-Zones-in-Windows-Azure-Cloud-Platform.aspx

ASP.NET MVC 4 部署到 Windows Azure 如何轉換時區設定的更多相关文章

  1. 使用ASP.NET MVC、Rabbit WeixinSDK和Azure快速开发部署微信后台

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:公众号后台系统和数据都基本准备妥当了,可以来分享下我是如何开发本微信公众号的后台系统了 ...

  2. 使用CSDN Code将网站部署到Windows Azure Website上

    在云计算时代,开发和部署应该是完全统一和集成的.在海外,开发者可以用github来管理他们的代码,并且直接部署到Windows Azure上.随着Windows Azure在国内的发布,我们发现,其实 ...

  3. 网站部署到Windows Azure Website上

    使用CSDN Code将网站部署到Windows Azure Website上 在云计算时代,开发和部署应该是完全统一和集成的.在海外,开发者可以用github来管理他们的代码,并且直接部署到Wind ...

  4. Redmine部署到Windows Azure

    有幸,今天可以尝试将Redmine部署到Windows Azure中,记下点滴,方便大家查阅 步骤一:Windows Azure中安装Ubuntu VM 遇到的问题,创建VM时会提示云服务.云存储订阅 ...

  5. ASP.NET Core教程:ASP.NET Core 程序部署到Windows系统

    一.创建项目 本篇文章介绍如何将一个ASP.NET Core Web程序部署到Windows系统上.这里以ASP.NET Core WebApi为例进行讲解.首先创建一个ASP.NET Core We ...

  6. 把Orchard部署到Windows Azure Web Sites

    很久前就想做个人站点,主要用来记录自己的生活,我喜欢摄影,烘焙…然后又刚刚入皮坑,这些都可以放在网站上展示一下,或许还能为自己带来收入. 然后手上刚好有Azure的试用,于是乎动力就上来了. 以下是部 ...

  7. 如何把ASP.NET MVC项目部署到本地IIS上

    默认情况下,在VisualStudio中开发网站,会运行在IISExpress中,如果想把网站部署到本地的IIS服务器上该怎么办呢? 一.首先,以管理员身份运行VisualStudio,否则在修改项目 ...

  8. ASP.NET CORE网站部署到 windows server 的IIS 上去

    章基于我自己经验的一个总结,在windows服务器上部署asp.net core网站.环境是 windows server 2012数据中心版本 第一步先安装 IIS 服务器 接下来就是一路下一步,然 ...

  9. 如何把网站及数据库部署到Windows Azure

    http://edi.wang/Post/2014/1/1/deploying-website-with-db-to-azure-custom-domain

随机推荐

  1. Android 抽屉类SlidingDrawer的使用

     比较简单,设置好SlidingDrawer控件的handle和content属性就可以了.  android:content="@+id/content"  android:ha ...

  2. jquery中的each方法,$.each \ this.each \ $.fn.each

    jquery.each 方法 方法一 $("img").each(function(i,elem){ // i 下标 从零开始, // elem == this // $(elem ...

  3. App开发者必备的运营、原型、UI设计工具整理

    移动应用开发者们,Attention please!在接下来的内容里,我们将介绍一些能够提高你们的应用的知名度和品质水平的工具. 仔细看看,下面这些工具你们都知道吗,用过吗?如果你还不知道,或许可以尝 ...

  4. 使用UpdatePanel 页面脚本不起作用

    在后台中这样注册js方法 ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "", " ...

  5. php反射机制获取未知类的详细信息

    使用ReflectionClass就可以获取未知类的详细信息 demo: require("hello.php"); $class = new ReflectionClass(&q ...

  6. 【java】之转码

    GBK->UTF-8 String str = "任意字符串"; str = new String(str.getBytes("gbk"),"u ...

  7. 拒绝了对对象 'sp_sdidebug'(数据库 'master',所有者 'dbo')的 EXECUTE 权限。

    如果在调试过程中出现异常“拒绝了对对象 'sp_sdidebug'(数据库 'master',所有者 'dbo')的 EXECUTE 权限.”则可以通过以下方式解决: 打开master数据库,打开扩展 ...

  8. Sqlserver 树形查询

    WITH tree AS(SELECT dwpid, dwid,1 AS x2level,dwmc,/*CAST(dwmc AS NVARCHAR(max)) x2name,*/CAST(+dwid ...

  9. 【转】 远程到服务器安装visualSVN server,出现Service 'VisualSVN Server' failed to start的解决方法

    在帮助远程到服务器上安装visualSVN server的时候,出现Service 'VisualSVN Server' failed to start. 解决方法(先不要关闭安装弹出的错误窗口): ...

  10. CSS实现背景透明,文字不透明(各浏览器兼容)

    /*CSS*/.waps{ background:url(07158.bmp) no-repeat top center fixed; width:1004px; text-align:center; ...