11

I tried adding namespaces to configuration/system.web/pages/namespaces in the web.config of my ASP.NET MVC 3 application so I could use classes in those namespaces in my views without needing a @using, however this has no effect. How can I add namespaces to my views?

3 Answers 3

28

MVC razor has a different area for namespaces.

Look in the second web.config, the one in your Views folder and add namespaces this way.

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="Telerik.Web.Mvc.UI;"/>
        </namespaces>
    </pages>
</system.web.webPages.razor>
Sign up to request clarification or add additional context in comments.

3 Comments

Heh, didn't even notice there was a web.config there. Works 100%, thanks and +1. Will accept in a few minutes.
Note, the below answer of closing and re-opening the view was also required before Visual Studio would stop giving me an error after adding the Resource folder to the namespace and re-building the solution.
great answer, you should remove the extra ';' <add namespace="Telerik.Web.Mvc.UI;"/> though. Thanks!
9

I'm not sure why it's so flaky, but chances are that it is actually working, but Visual Studio doesn't recognize it until you close and re-open the view you're in. Also make sure you're in the web.config that's located in the Views directory.

4 Comments

Tried that, no dice. ;) I made sure to try uploading and executing the page before posting, and it failed to compile the view.
So you're modifying the web.config at the root of the Views directory like @DisplacedGuy recommended? I think that should take care of it. Also make sure you're in the system.web.webPages.razor hierarchy if that's the view engine you're using.
Yeah my original problem was that I was using configuration/system.web/pages/namespaces and not system.web.webPages.razor as @DisplacedGuy suggested.
After two hours without making it work, I realized your post, closed the views, reopened and it worked... thanks for your observations ataddeini
1

I used this method to include Resource files into my views but Razor didn't want to pick up my classes. After some hair-pulling I realized I didn't change the Access Modifier on the Resources.resx file to public (it's created as Internal by default). Once I changed it & recompiled, I could access my resources from all views.

Rookie mistake, hate to see it happen, but hopefully it'll save someone some pain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.