Namespace Not Found in cshtml table in Views / Shared MVC 5

Using MVC 5.

In web.config in the root, I have this namespace defined:

  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
    <pages>
      <namespaces>
        <add namespace="Westwind.Globalization" />
      </namespaces>
...

And in my views / shared folder, I have .cshtml file with this code in it:

@DbRes.T("SaleAnnouncement")

Which accesses a method within the Westwind.Globalization class.

MVC doesn’t find the namespace indicated in the root web.config on the .cshtml page, and it triggers the intellisense validation error. 

So then I tried adding it in the same place to the web.config file in the Views folder and if found the method that time around, but still the intellisense validation flagged the method call as an error on the page. 

So is there a way to add the namespace to the root web.config and have it be resolved to all view pages on the site–without triggering the intellisense validation?     

Also, is there a need to have a web.config file at the Views level at all?   I would rather just have a 1 global web.config file for the entire site?

Thanks for your help,

Ian

Hi Ian,

Base on my test in MVC 5 with ASP.NET MVC 5.0, we can add the namespace to the web.config that in corresponding folder. (e.g. web.config in the view folder)

However, for this way, it can’t passes the intellisense validation. (it works well during the runtime)

Base on my test in MVC 5 with ASP.NET MVC 5.1, it works with intellisense if I follow up these steps below:

  1. Add namespace to web.config that in Views folder.
  2. Rebuild the project
  3. Add new view

Please try to add namespace to the web.config first, then rebuild it and add a new view to check the result.

iforsythCP

Also, is there a need to have a web.config file at the Views level at all?   I would rather just have a 1 global web.config file for the entire site?

The web.config that in the view level is needed.

Best Regards

Starain

Thanks Starain. 

Just an update: 

I looked at the MVC version that I was using and it was actually ver. 5.2.0.   

Once the namespaces are added to the web.config file on the Views level, the intellisense works fine in a View under the VIews/<My Custom View Name Folder>; however, it does NOT seem to work on any layout created under Views/Shared directory.

Ian

Hi Ian,

Base on my test with MVC 5.2.0, if I add the namespace to the web.config (in views folder), then add a new view in the Shared folder, the
intellisense works fine.

If I don’t add the namespace to the web.config (view folder) and add a new view, then the intellisense don’t work.

So, I suggest that you could add the namespace to the web.config first, then create a new view.

Best Regards

Starain

Leave a Reply