RubbelDieKatz
RubbelDieKatz

Reputation: 1187

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

I'm trying to add Localization to my ASP.NET MVC application. However, I'm already failing at including the necessary packages. I'm fairly certain that my application is using Views, and I'm about 50% sure that it's using Razor pages. At least the Razor pages syntax works, except in this case.

I'm following this official Microsoft tutorial and this one. Both appear to use about the same code that I'm using. I might be missing a crucial package, though.

Here's part of my _Layout.cshtml.

@using Microsoft.AspNetCore.Mvc.Localization
@using System.Threading.Tasks

@inject IViewLocalizer Localizer

<!DOCTYPE html>

The first two @using lines work flawlessly. The fourth line throws an error and the page shows an error in the browser.

CS0103: The name 'inject' does not exist in the current context

Upvotes: 1

Views: 4112

Answers (1)

RubbelDieKatz
RubbelDieKatz

Reputation: 1187

So it turns out I was missing the .NET Core development environment. You can install it via the Visual Studio installer.

  • Run the Visual Studio installer
  • Click "Change" to change your existing Visual Studio installation
  • Add the .NET Core Extension
  • Click "Change" to apply your changes
  • Your extension should download automatically.

Note that this will not necessarily turn your existing ASP.NET application into a Core application.

Regular old AST.NET MVC localization appears to be quite easy. (Archive link just in case)

  • Add a .resx file
  • Reference that file and its resources in your view like this:

@Resources.<Name of your file>.<Name of the resource line>

Example:

<h3>@Resources.Global.AppName</h3>

Upvotes: 4

Related Questions