2

I use the localization paradigm of one resx file per view in all my projects. I have two identical folder structures for Resources and Views and I link the resource and the view like so:

@using Res = [Namespace].Resources.[Controller].[View]
<p>@Res.[ResourceKey]</p>

Is it possible to get asp.net to use/link the resource file without the using statements and defining a Custom Tool Namespace for all my Resx files? (Yes, I am that lazy.)

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100

1 Answers1

1

Open your web.config in your Views folder.

enter image description here

Add the namespace for your resources.

enter image description here

Restart VS as it doesn't pick up the namespace so it's not showing intellisense.

Call your resources from any view without adding using statements inside your *.cshtml files.

Matija Grcic
  • 12,963
  • 6
  • 62
  • 90
  • Looks interesting! I tried to add it but wound up with questions; would I have to add this for all my view folders? Currently I use /Views/[Controllername]/[Viewname].cshtml and /Resources/[Controllername]/[Viewname].resx, would I have to refactor it? Wouldn't that also mean that I have all my resources in available in all views? This is something I can't do as my resource keys overlap a lot; for example many views contain a different meaning for a "TabInfo" key. I have at least 10 different "Index.cshtml" views and "Index.resx" resource files, would that not cause a collision? – Joel Peltonen Jan 30 '13 at 12:58
  • So you basically you have a structure by convention (for every Views folder a Resources folder in it), am i correct? There will be no collision because you would use different namespace for a different view. So lets say you have a View called UserLogin.cshtml, and a Resources file UserLogin.resx. Then you would call it UserLogin.[Key]. It really depends how big is your application. I personally use using statements, as it's cleaner and easier to maintain. – Matija Grcic Jan 30 '13 at 23:27
  • Sorry I don't. I have a Views folder and a separate Resources folder. I'm trying http://www.codeproject.com/Articles/181738/Creating-a-Bilingual-ASP-NET-MVC3-Application-Part but I added the `@using` that I have. If I did use the folder structure you mentioned and had `` What if both Resource folders had "Edit.resx"? This http://stackoverflow.com/a/5510110/694325 is very similar to mine but I want a more dynamic solution :) – Joel Peltonen Jan 31 '13 at 07:27