tags:

views:

450

answers:

2

How to import a namespace in Razor View Page?

+16  A: 

Finally found the answer.

@using MyNamespace;
Amitabh
+7  A: 

In ASP.NET MVC 3 Preview1 you can import a namespace on all your razor views with this code in Global.asax.cs

Microsoft.WebPages.Compilation.CodeGeneratorSettings.AddGlobalImport("Namespace.Namespace");

I hope in RTM this gets done through Web.config section.

Germán
Sweet. Love configuring stuff in code.
Schotime
There will be a web.config section in RTM, but we also wanted to provide an API to do this because many users are starting to gravitate away from config. So we have both options available for you!
anurse
As of ASP.NET MVC 3 Beta this method no longer works. There is a new web.config section as explained here http://stackoverflow.com/questions/3875207/razor-syntax-using-and-namespace-declarations-fail . The AddGlobalImport method for importing a global namespace to all views has been moved to this class System.Web.WebPages.Razor.WebPagesRazorHost
Germán