這我如何固定我的項目:
- 增加了一個類,將同時能延長該RazorViewEngine(繼承自它)。
我使用(以RazorViewEngine從WebFormsViewEngine,我的應用程序的文化餅乾和兩個字母languge從文化中提取)的微小變化在布賴恩·瑞特後的代碼(以上鍊接):
使用系統; using System.Collections.Generic;使用System.Linq的 ; using System.Web; using System.Web.Mvc; using System.Text.RegularExpressions;使用System.IO的 ; 使用LeadsWize.Helpers;使用System.Globalization的 ;
命名空間System.Web.Mvc { 公共類GlobalizationViewEngine:RazorViewEngine { 保護覆蓋IVIEW CreatePartialView(ControllerContext controllerContext,串partialPath) { partialPath = GlobalizeViewPath(controllerContext,partialPath); 返回新的RazorView(controllerContext,partialPath,null,false,FileExtensions,ViewPageActivator); }
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
viewPath = GlobalizeViewPath(controllerContext, viewPath);
return base.CreateView(controllerContext, viewPath, masterPath);
}
private static string GlobalizeViewPath(ControllerContext controllerContext, string viewPath)
{
var request = controllerContext.HttpContext.Request;
string cultureName = null;
HttpCookie cultureCookie = request.Cookies["_culture"];
if (cultureCookie != null)
cultureName = cultureCookie.Value;
else
cultureName = request.UserLanguages != null && request.UserLanguages.Length > 0 ?
request.UserLanguages[0] : // obtain it from HTTP header AcceptLanguages
null;
// Validate culture name
cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe
var lang = (CultureInfo.CreateSpecificCulture(cultureName)).TwoLetterISOLanguageName; // this is to extract the two languge letters from the culture
if (lang != null &&
!string.IsNullOrEmpty(lang) &&
!string.Equals(lang, "en", StringComparison.InvariantCultureIgnoreCase))
{
string localizedViewPath = Regex.Replace(
viewPath,
"^~/Views/",
string.Format("~/Views/Globalization/{0}/",
lang
));
if (File.Exists(request.MapPath(localizedViewPath)))
{ viewPath = localizedViewPath; }
}
return viewPath;
}
}
}
添加有兩行Application_Stat在Global.asxs.cs
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new GlobalizationViewEngine()); //this is our customized extended view engine to support the globaliztion in view folder Globalization.
**請注意,文件夾arangemnet爲全球化視圖文件與Brian的文章完全相同。
博客中的示例與MVC相關,而不是Web表單。但我想你使用Razor,所以你可以使用與例子中相同的代碼來擴展你的RazorViewEngine。 – Egor4eg
謝謝@ Egor4eg你是對的。 – Shlo