0

In ASP.NET MVC4, when I want to use a C# class I am forced to use the full package name. For example, whereas I normally use:

using MyClasses.More.ExObject;

ExObject myObj = new Exobject();

I am forced to specify the namespace explicitly:

MyClasses.More.ExObject myObj = new MyClasses.More.ExObject();

Why?

John 'Mark' Smith
  • 2,564
  • 9
  • 43
  • 69

1 Answers1

6

If you open the Web.Config inside the Views folder, you can add namespaces there as well, so you don't have to add the using to the top of any Views.

However, at the top of your Razor view, don't forget the syntax is @using, not just using, which may be the reason that it's not working for you.

EDIT

Inside your Web.config (again, the one inside the Views folder, not the one at the bottom of the project), you can add additional namespaces in the following location:

<system.web.webPages.razor>
    <host... />
    <pages>
        <!-- These are added by default when you create the MVC project -->
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
        <!-- Add your additional namespaces here -->
    </pages>
</system.web.webPages.razor>

This section is towards the top, just below the <configSections> area. (The project I have is on line 11 without having added anything to the file above it.

As a side note, if you're using Visual Studio 2010, I typically need to close all of my views and then restart Visual Studio to get it to update those. Keep that in mind if it doesn't work immediately.

krillgar
  • 12,596
  • 6
  • 50
  • 86
  • Can you update this answer to show where in the views folder's web.config one would add the namespaces they want to include in the views? If someone is having questions on the `using` statement in razor, plowing through a 60+ line XML file to add a namespace might also be troubling. – Tommy Feb 11 '14 at 13:50
  • Agreed. Done. As I said in my edit, those namespaces are added by default. – krillgar Feb 11 '14 at 13:55