1

I have separate Resources assembly and i want every razor view to have access to it.

Now i put @using Res = MyProject.Web.Resources; in every view that needs it (later all pages will) and access the resources by

@Res.TheNeededResource.TheField

I tried to add the resouce namespace to web.config in view folder with no success

<add namespace="MyProject.Web.Resources" /> 

if i add full path namespace in view IDE highlights the namespace i've added in config as redundant - when i remove it - error

How to add that namespace correctly? or some other way?

Kiril Kolev
  • 158
  • 1
  • 15

1 Answers1

1

In your web.config, most likely the file in the Views folder, you can add a namespace with an alias like this:

<namespaces>
    ...
    <add namespace="Res=MyProject.Web.Resources" />
</namespaces>

Now you can call your code as you would before:

@Res.TheNeededResource.TheField

This is an undocumented feature of Razor and the IDE may render it as an error even though it is valid syntax. This means you may be better off changing the base page type to your own, like this:

public abstract class BaseViewPage : BaseViewPage<object>
{
}

public abstract class BaseViewPage<TModel> : WebViewPage<TModel>
{
    public TheNeededResource Res => MyProject.Web.Resources.TheNeededResource
}

In your web.config change the base page type to your own:

<pages pageBaseType="MyProject.Web.BaseViewPage">

Now in your views you can use the Res Property just like you use Html:

@Res.TheField
DavidG
  • 113,891
  • 12
  • 217
  • 223