asp.net-mvc 如何使用asp.net mvc razor在视图中访问全局资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11399068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:05:31  来源:igfitidea点击:

How to access global resources in a view using asp.net mvc razor

asp.net-mvcrazorlocalizationglobalization

提问by FiveTools

Using ASP.NET MVC Razor, I have a resource file in App_GlobalResourcesnamed General.resxand General.fr.resxwith a name value pairings of "Hello" and "Hello" and "Hello" and "Bonjour".

使用ASP.NET MVC剃刀,我有一个资源文件App_GlobalResources命名General.resx,并General.fr.resx用“你好”和“你好”和“你好”和“卓悦”的名称值配对。

How do I reference these from my view?

我如何从我的角度参考这些?

These do not work:

这些不起作用:

@Html.Resource("General.Hello")
@Html.Resource("GlobalResources.Hello")
@Html.Resource("GlobalResources.General.Hello")
@Html.Resource("GlobalResources, General.Hello") 

回答by VJAI

Try this,

尝试这个,

@Resources.General.Hello

syntax: Resources.[ResourceName].[Property]

语法:Resources.[ResourceName].[Property]

回答by WholeLifeLearner

you need to refer to your namespace:

你需要引用你的命名空间:

@GeneralResxNamespace.General.Hello

You can do this in an easier way by adding namespace to /Views/Web.config file In section with namespaces add section below:

您可以通过将命名空间添加到 /Views/Web.config 文件中以更简单的方式做到这一点,在下面的命名空间部分添加部分:

<add namespace="*YourProjectNamespace*.*ResourcesNamespace*.Resources" />

Then you can use resources without namespace, like in the example:

然后您可以使用没有命名空间的资源,如示例中所示:

General.Hello

回答by Arun Prasad E S

To access value from resoure file in view

在视图中访问资源文件中的值

Add this name space

添加这个命名空间

@using System

Then display the value like this

然后像这样显示值

@ResourceFile.Address  (ResouceFile is the name of the ResouceFile)

This method is used when evironment culture thread is used.

使用环境培养线时使用此方法。