c# - ASP.NET MVC View : The name 'inject' does not exist in the current context

标签 c# asp.net asp.net-mvc razor dependency-injection

我正在尝试将本地化添加到我的 ASP.NET MVC 应用程序中。但是,我已经无法包含必要的软件包。我相当确定我的应用程序正在使用 View ,而且我有大约 50% 的把握它正在使用 Razor 页面。至少 Razor 页面语法有效,但在这种情况下除外。

我正在关注 this official Microsoft tutorialthis 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/

相关文章:

asp.net-mvc - 每个请求的匿名标识 cookie 更改

c# - 当需要处理大量内存集合时,有什么技巧可以减少内存消耗吗?

c# - 在 Visual Studio 上打开自定义工具窗口,而不从“ View ”菜单调用它

asp.net - 在 Visual Studio 2012 上预编译 Azure WebRole 发布(不通过 WebDeploy)

asp.net - ASP.Net/C#,在页面上的某些控件之间循环?

c# - 构造函数 Controller 中的 MVC 异步方法

c# - 在 C# 中使用数组填充 Xaml

c# - EF 或 SQL 是审计数据更改的更好选择吗?

c# - Sys.WebForms.PageRequestManagerServerErrorException : Input string was not in a correct format

html - Bootstrap3 mvc 5 - 中心表格 div