如何在 ASP.NET Web 应用程序项目中使用它们?有什么区别吗?
非常感谢
如何在 ASP.NET Web 应用程序项目中使用它们?有什么区别吗?
非常感谢
本地资源:
例如:Default.aspx.resx- 基础资源文件。这是默认或备用资源文件。
Default.aspx.de.resx - 德语等资源文件。
全球资源:
全局资源必须存储在应用程序根目录的 App_GlobalResources 中。
// Get the global resource string.
try
{
globalresourcestring = (String)GetGlobalResourceObject("MyResource", "GlobalResourceString1");
}
catch
{
globalresourcestring = "Could not find global resource.";
}
这些是 ASP.NET MVC 5 Web 应用程序的步骤
前任。
<add namespace="ProjectName.App_LocalResources" />
为了在您的 html 代码中访问本地资源文件,您必须添加
@using ProjectName.App_LocalResources
到页面顶部。
然后,您可以使用 App_LocalResources 访问不同的资源
@NameOfResxFile.NameOfValueInResxFile
我希望这有帮助。
如果您非常确定您的资源存在于您的 .resx 文件中,您可以尝试:
string myValue = HttpContext.GetGlobalResourceObject("MyResourceFile","MyResource").ToString();
如果您不确定您的资源是否存在,您可以使用:
var resultMessage = HttpContext.GetGlobalResourceObject("MyResourceFile","MyResource");
string myValue = resultMessage == null ? string.Empty : resultMessage.ToString();