How to import a namespace in Razor View Page?
+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
2010-07-28 06:47:37
Sweet. Love configuring stuff in code.
Schotime
2010-07-30 01:14:01
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
2010-07-30 18:23:54
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
2010-10-07 22:20:26