Register Global Namespaces in ASP.NET MVC 3 Preview 1 and ASP.NET WebForms
Posted by: Malcolm Sheridan
in Category ASP.NET MVC
Abstract: The following article demonstrates how to register global namespaces in ASP.NET MVC 3 Preview 1.
From time to time you find little gold nuggets that you had no idea existed. This is a gold nugget that you can not only use in ASP.NET MVC 3 Preview 1, but also ASP.NET WebForms. If you use the Razor view engine, there's no simple way to register a global namespace. For example, this code will work:
The following code won’t work because the System.Globalization namespace hasn’t been imported into the view:
If I wanted to register the System.Globalization namespace inside a view, this won’t work as there’s no syntax for this:
<%@ Import Namespace="System.Globalization" %>
Currently in Razor there is no page directive syntax to do that. A second option would be to register the namespace in the pages element in the web.config file.
This is the preferred option in my opinion because it allows you to centralize the namespace registrations. This however doesn't work with the Razor view engine currently. Fingers crossed this will work when it’s released.
If you want to import a global namespace into the current Razor views, it’s a simple two step process. Open the AssemblyInfo file under the Properties folder and add the following line of code:
[assembly: PreApplicationStartMethod(typeof(PreStartupCode), "Starting")]
PreApplicationStartMethod is a new attribute in .NET 4.0 that allows you to have code run way early in the ASP.NET pipeline as an application starts up. Way early, even before Application_Start. The first argurment is the type of class to be run. This is a static class that I have called PreStartupCode. The second argument is the method to run. Here’s the PreStartupCode class:
public static class PreStartCode
public static void Starting()
The code above is using the new CodeGeneratorSettings class in .Net 4.0. This gives you the power to inject settings, such as global namespaces, into your project. If you try the following code now, it will work:
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!