Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing resource files from a razor view

I use ASP.NET Internationalization from Code52 at http://code52.org/aspnet-internationalization/ which uses (language) resource files.

From my controller I reference the string I want like this

ViewBag.Message = Language.Index_Title;

Is it possible to reference the values directly in the (razor) view? Something like this: @Resources.Language.Index_Title;

like image 990
Christer Kolterjahn Avatar asked Mar 28 '12 15:03

Christer Kolterjahn


People also ask

Which namespace is used for razor view?

The namespace for Razor Engine is System. The Razor file extension is "cshtml" for the C# language. By default, Razor View Engine encodes html tags or scripts before it's being rendered to view that avoids Cross-Site Scripting attacks.

What is a razor file?

Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.


1 Answers

In your web.config you have the "Pages" section... there you need to add your Resource namespace... that way you don't need to declare it in every page using "using".

<pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
    <add namespace="Your.Resources.Namespace" />  <!--ADD THIS LINE-->
  </namespaces>
</pages>

Then, assuming your resource file is called "Language", in your Razor view you use:

@Language.Index_Title
like image 197
Romias Avatar answered Sep 18 '22 22:09

Romias