我正在尝试将本地化添加到我的 ASP.NET MVC 应用程序中。但是,我已经无法包含必要的软件包。我相当确定我的应用程序正在使用 View ,而且我有大约 50% 的把握它正在使用 Razor 页面。至少 Razor 页面语法有效,但在这种情况下除外。
我正在关注 this official Microsoft tutorial和 this one .两者似乎都使用了与我正在使用的相同的代码。不过,我可能遗漏了一个关键包。
这是我的 _Layout.cshtml
的一部分。
@using Microsoft.AspNetCore.Mvc.Localization
@using System.Threading.Tasks
@inject IViewLocalizer Localizer
<!DOCTYPE html>
前两行 @using
完美无缺。第四行抛出错误,页面在浏览器中显示错误。
CS0103: The name 'inject' does not exist in the current context
最佳答案
原来我缺少 .NET Core 开发环境。您可以通过 Visual Studio 安装程序安装它。
- 运行 Visual Studio 安装程序
- 单击“更改”以更改现有的 Visual Studio 安装
- 添加 .NET Core 扩展
- 点击“更改”以应用您的更改
- 您的扩展应该会自动下载。
请注意,这不一定会将您现有的 ASP.NET 应用程序转变为核心应用程序。
Regular old AST.NET MVC localization appears to be quite easy. ( Archive link just in case )
- 添加 .resx 文件
- 在您的 View 中引用该文件及其资源,如下所示:
@Resources.<Name of your file>.<Name of the resource line>
例子:
<h3>@Resources.Global.AppName</h3>
关于c# - ASP.NET MVC View : The name 'inject' does not exist in the current context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52326007/