C# 如何访问本地资源:MVC
我跨线程询问如何访问本地资源,他解决了自己的问题,但我的情况有点不同C# 如何访问本地资源:MVC,c#,asp.net-mvc,localization,resources,C#,Asp.net Mvc,Localization,Resources,我跨线程询问如何访问本地资源,他解决了自己的问题,但我的情况有点不同 Areas>Models>Support>Localization MyResouces.resx SupportModel 在我的SupportModel中,我希望访问本地化/myresources.resx中的值。语法是什么 一种解决方案是使用HttpContext.GetLocalResourceObj
Areas>Models>Support>Localization
MyResouces.resx
SupportModel
在我的SupportModel中,我希望访问本地化/myresources.resx中的值。语法是什么
一种解决方案是使用HttpContext.GetLocalResourceObjec(虚拟路径,键),但它对我不起作用。我用过
GetLocalResourceObject("~/Areas/Models/Support/SupportModel", "option1Text")
我还将本地化文件夹重命名为App_LocalResources,但这并没有解决问题。我可能做错什么了吗
请注意,我已经修改了我的代码,因为我不想发布真正的代码。Thx我创建了一个示例应用程序,其中模型/支持中包含Resource1.resx文件,这就是我从中获取值的方式:
public ActionResult Login(string returnUrl)
{
var res = Models.Support.Resource1.TestString1; // this is resource created inside Models/Support/
ViewBag.ReturnUrl = returnUrl;
return View();
}
要从本地化/myresources.resx访问值,请尝试以下操作:
@Areas.Models.Support.Localization.MyResouces.option1Text
语法:[命名空间].[ResourceName].[Property]
假设您的myresources.resx文件具有
public
access修饰符。我使用的解决方案是这样的。有趣的是,没有在线解决方案
using Myproject.Areas.Models.Support.Localization;
...
MyResouces.option1
以上不是实际编译的代码。您可以使用以下语法在视图中访问它:
@Resources.IndexPage.PropertyName
您必须确保资源文件具有以下属性:
构建操作:嵌入式资源
自定义工具:PublicResXFileCodeGenerator
自定义工具名称空间:Resources.IndexPage(这是您给定的名称空间,在视图中用于访问属性
看看这篇文章: