亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ASP.NET?MVC實現(xiàn)本地化和全球化

 更新時間:2022年10月25日 08:33:46   作者:Darren?Ji  
這篇文章介紹了ASP.NET?MVC實現(xiàn)本地化和全球化的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

在開發(fā)多語言網(wǎng)站時,我們可以為某種語言創(chuàng)建一個資源文件,根據(jù)瀏覽器所設置的不同語言偏好,讓運行時選擇具體使用哪個資源文件。資源文件在生成程序集的時候被嵌入到程序集。

本篇體驗,在ASP.NET MVC中實現(xiàn)全球化和本地化,比如,當瀏覽器選擇英文,就讓某些頁面元素顯示英文;當瀏覽器選擇用中文瀏覽,則顯示中文。

使用Visual Studio 2013創(chuàng)建一個無身份驗證的MVC項目。

創(chuàng)建如下的Model:

    public class Student
    {
        public int Id { get; set; }
        [Display(Name="姓名")]
        [Required(ErrorMessage="必填")]
        public string Name { get; set; }
        [Display(Name = "年齡")]
        [Required(ErrorMessage = "必填")]
        public int Age { get; set; }
    }

生成解決方案。

在HomeController中Index方法中添加一個有關Student的強類型視圖,并選擇默認的Create模版。大致如下:

@model GlobalAndLocal.Models.Student
<h2>Index</h2>
<div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
</div>
<div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="創(chuàng)建" class="btn btn-default" />
            </div>
</div>

現(xiàn)在,我們希望,當瀏覽器選擇英語的時候,頁面元素都顯示英文。

在解決方案下創(chuàng)建一個名稱為MyResources的類庫。

創(chuàng)建有關中文的資源文件,并把訪問修飾符設置為public:

創(chuàng)建有關英文的資源文件,也把訪問修飾符設置為public:

生成類庫。

在MVC項目中引用該類庫。

修改Student類如下:

    public class Student
    {
        public int Id { get; set; }
        [Display(Name=MyResources.Resource.Name)]
        [Required(ErrorMessage=MyResources.Resource.NameRequiredError)]
        public string Name { get; set; }
        [Display(Name = MyResources.Resource.Age)]
        [Required(ErrorMessage = MyResources.Resource.AgeRequiredError)]
        public int Age { get; set; }
    }

在Index強類型視圖頁中,修改如下:

<h2>@MyResources.Resource.IndexHeader</h2>
<div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
</div>
<div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                 <input type="submit" value="@MyResources.Resource.Submit" class="btn btn-default" />
            </div>
</div> 

運行MVC項目,出現(xiàn)報錯。

修改Student類如下:

    public class Student
    {
        public int Id { get; set; }
        [Display(Name="Name", ResourceType=typeof(MyResources.Resource))]
        [Required(ErrorMessageResourceName = "NameRequiredError", ErrorMessageResourceType = typeof(MyResources.Resource))]
        public string Name { get; set; }
        [Display(Name = "Age", ResourceType = typeof(MyResources.Resource))]
        [Required(ErrorMessageResourceName = "AgeRequiredError", ErrorMessageResourceType = typeof(MyResources.Resource))]
        public int Age { get; set; }
          
    }

最后,還需要在Web.config中設置如下:

  <system.web>
    ......
    <globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true"></globalization>  
  </system.web>

在chrome瀏覽器語言設置中選擇英語。

刷新后,效果如下:

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

相關文章

最新評論